OneSignal-Android-SDK
OneSignal-Android-SDK copied to clipboard
Error updating from onesignal 3.x.x to 4.x.x sdk
Description:
I am a newbie in android studio/java programming, I have an already built source code for a webview application. I updated the onesignal sdk from 'com.onesignal:OneSignal:[3.0.0, 3.99.99]' to the 'com.onesignal:OneSignal:[4.0.0, 4.99.99]', but I got an error in my app.java when trying to sync the gradle and building the app:
My build.gradle:
buildscript {
repositories {
maven { url 'https://plugins.gradle.org/m2/'}
}
dependencies {
classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:0.14.0'
}
}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
apply plugin: 'com.android.application'
android {
compileSdkVersion 31
buildToolsVersion '30.0.2'
defaultConfig {
applicationId 'com.ortho.fixar'
minSdkVersion 16
targetSdkVersion 31
versionCode 4
versionName "1.3"
multiDexEnabled true
// manifestPlaceholders = [
// onesignal_app_id: '',
// onesignal_google_project_number: 'REMOTE']
}
signingConfigs {
release {
storeFile file("")
storePassword ""
keyAlias ""
keyPassword ""
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
dexOptions {
preDexLibraries = false
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'com.google.firebase:firebase-analytics:18.0.1'
//implementation "com.google.android.gms:play-services-gcm:$gps_version"
//implementation "com.google.android.gms:play-services-ads:$gps_version"
//implementation "com.google.android.gms:play-services-analytics:$gps_version"
implementation 'com.onesignal:OneSignal:[4.0.0, 4.99.99]'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'com.google.firebase:firebase-ads:18.3.0'
implementation 'com.google.android.gms:play-services-ads:18.3.0'
}
apply plugin: 'com.google.gms.google-services'
my app.java code:
package com.ortho.fixar;
import android.content.Intent;
import android.net.Uri;
import androidx.multidex.MultiDexApplication;
import android.text.TextUtils;
import android.util.Log;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import com.ortho.fixar.activity.MainActivity;
import org.json.JSONObject;
public class App extends MultiDexApplication {
private String push_url = null;
private FirebaseAnalytics mFirebaseAnalytics;
@Override public void onCreate() {
super.onCreate();
if (Config.ANALYTICS_ID.length() > 0) {
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
}
//OneSignal Push
if (!TextUtils.isEmpty(getString(R.string.onesignal_app_id)))
OneSignal.init(this, "REMOTE", getString(R.string.onesignal_app_id), new NotificationHandler());
}
// This fires when a notification is opened by tapping on it or one is received while the app is running.
private class NotificationHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
@Override
public void notificationOpened(OSNotificationOpenResult result) {
try {
JSONObject data = result.notification.payload.additionalData;
String webViewUrl = (data != null) ? data.optString("url", null) : null;
String browserUrl = result.notification.payload.launchURL;
if (webViewUrl != null || browserUrl != null) {
if (browserUrl != null || result.notification.isAppInFocus) {
browserUrl = (browserUrl == null) ? webViewUrl : browserUrl;
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(browserUrl));
startActivity(browserIntent);
Log.v("INFO", "Received notification while app was on foreground or url for browser");
} else {
push_url = webViewUrl;
}
} else if (!result.notification.isAppInFocus) {
Intent mainIntent;
mainIntent = new Intent(App.this, MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mainIntent);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
public synchronized String getPushUrl(){
String url = push_url;
push_url = null;
return url;
}
public synchronized void setPushUrl(String url){
this.push_url = url;
}
}
Environment
I am using android sdk 31, and downloaded the sdk using maven.
Steps to Reproduce Issue:
- I added the one signla sdk 'com.onesignal:OneSignal:[4.0.0, 4.99.99]'
- and already placed App id
- error
This issue is comparable to the previous one so check this one #1399 Hope you find the solution exact.
This issue is comparable to the previous one so check this one #1399 Hope you find the solution exact.
this didn't help me... i just need to update the code in app.java but i don't know how
@mhmadfarooq Need some improvements as dependencies upgraded such as
import com.onesignal.OSNotificationOpenResult; //deprecated
import com.onesignal.OSNotificationOpenedResult; //new
import com.onesignal.OneSignal;
public class App extends MultiDexApplication {
private String push_url = null;
@Override
public void onCreate() {
super.onCreate();
//OneSignal Push
OneSignal.initWithContext(this);
OneSignal.setAppId(R.string.one_signal_app_id);
OneSignal.setNotificationOpenedHandler(new NotificationHandler());
}
private class NotificationHandler implements OneSignal.OSNotificationOpenedHandler {
@Override
public void notificationOpened(OSNotificationOpenedResult result) {
try {
JSONObject data = result.getNotification().getAdditionalData();
String webViewUrl = (data != null) ? data.optString("url", null) : null;
String browserUrl = result.getNotification().getLaunchURL();
if (webViewUrl != null || browserUrl != null) {
if (browserUrl != null) {
browserUrl = (browserUrl == null) ? webViewUrl : browserUrl;
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(browserUrl));
startActivity(browserIntent);
Log.v("INFO", "Received notification while app was on foreground or url for browser");
} else {
push_url = webViewUrl;
}
} else {
Intent mainIntent;
mainIntent = new Intent(App.this, MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mainIntent);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
Note:- Some of the code removed. You may use it to your requirements. Hope it work
For further details you should read documentation OneSignalSDK Android Notification Event Handlers.
Tha
@mhmadfarooq Need some improvements as dependencies upgraded such as
import com.onesignal.OSNotificationOpenResult; //deprecated import com.onesignal.OSNotificationOpenedResult; //new import com.onesignal.OneSignal; public class App extends MultiDexApplication { private String push_url = null; @Override public void onCreate() { super.onCreate(); //OneSignal Push OneSignal.initWithContext(this); OneSignal.setAppId(R.string.one_signal_app_id); OneSignal.setNotificationOpenedHandler(new NotificationHandler()); } private class NotificationHandler implements OneSignal.OSNotificationOpenedHandler { @Override public void notificationOpened(OSNotificationOpenedResult result) { try { JSONObject data = result.getNotification().getAdditionalData(); String webViewUrl = (data != null) ? data.optString("url", null) : null; String browserUrl = result.getNotification().getLaunchURL(); if (webViewUrl != null || browserUrl != null) { if (browserUrl != null) { browserUrl = (browserUrl == null) ? webViewUrl : browserUrl; Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(browserUrl)); startActivity(browserIntent); Log.v("INFO", "Received notification while app was on foreground or url for browser"); } else { push_url = webViewUrl; } } else { Intent mainIntent; mainIntent = new Intent(App.this, MainActivity.class); mainIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(mainIntent); } } catch (Throwable t) { t.printStackTrace(); } } } }
Note:- Some of the code removed. You may use it to your requirements. Hope it work
For further details you should read documentation OneSignalSDK Android Notification Event Handlers.
Thank u so much.. this solve some problems but i got an error in these parts of code :
OneSignal.setNotificationOpenedHandler(new NotificationHandler());
it gives me an error of (Cannot resolve symbol 'NotificationHandler')
and in this part:
private class NotificationHandler implements OneSignal.OSNotificationOpenedHandler {
it gives me an error (Modifier 'private' not allowed here)...
what could be the problem?!
thank you
@mhmadfarooq No there is no error, it works fine. You should clean your project so old dependency can be cleared from cache. Would you like to tell which of the IDE you are using? If you are using Android Studio then go Build >Clean Project.
If still not solve then grab screenshot and feel free to share it. Thanks.
@mhmadfarooq No there is no error, it works fine. You should clean your project so old dependency can be cleared from cache. Would you like to tell which of the IDE you are using? If you are using Android Studio then go Build >Clean Project.
If still not solve then grab screenshot and feel free to share it. Thanks.
you are right, the error disappeared, but the doesn't get any notification.
Hi @mhmadfarooq, happy to see you got help with your error.
Are you still having trouble receiving notifications on your device? Are you sending via the OneSignal dashboard or the API?
@mhmadfarooq No there is no error, it works fine. You should clean your project so old dependency can be cleared from cache. Would you like to tell which of the IDE you are using? If you are using Android Studio then go Build >Clean Project. If still not solve then grab screenshot and feel free to share it. Thanks.
you are right, the error disappeared, but the doesn't get any notification.
Did you solved this or not yet? I'm facing the same problem.
Closing this issue as it has become stale. Please @ mention me if we need to revisit this issue.
We have an updated major release available for our Android SDK with many improvements and enhancements! Please refer to the migration guide for more information on upgrading.
Thanks!