OneSignal-Android-SDK
OneSignal-Android-SDK copied to clipboard
How to open links in the corresponding page in webview android application and not in browser
Description:
I'm using One signal push notification but I want the notifications to open in the URL that is sent with it but not the homepage
This is my app class:
private String push_url = null;
@Override
public void onCreate() {
super.onCreate();
// OneSignal Initialization
OneSignal.initWithContext(this);
OneSignal.setAppId(ONESIGNAL_APP_ID);
//OneSignal Push
OneSignal.initWithContext(this);
OneSignal.setAppId(String.valueOf(R.string.ONESIGNAL_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();
}
}
}
public synchronized String getPushUrl(){
String url = push_url;
push_url = null;
return url;
}
public synchronized void setPushUrl(String url){
this.push_url = url;
}
}
This is my manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Grinta"
android:theme="@style/AppTheme"
android:supportsRtl="true"
android:name="com.drbrandat.grinta.App"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<meta-data
android:name="com.google.android.gms.ads.AD_MANAGER_APP"
android:value="true"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data android:name="com.onesignal.suppressLaunchURLs" android:value="true"/>
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="grinta_chanel"/>
<meta-data android:name="com.onesignal.NotificationServiceExtension"
android:value="com.onesignal.example.NotificationServiceExtension" />
<activity
android:name="com.drbrandat.grinta.activity.MainActivity"
android:configChanges="keyboardHidden|keyboard|orientation|screenSize"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!--
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:scheme="http" android:host="mysite.com" ></data>
<data android:scheme="http" android:host="*.mysite.com" ></data>
<data android:scheme="https" android:host="mysite.com" ></data>
<data android:scheme="https" android:host="*.mysite.com" ></data>
</intent-filter>-->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="http" android:host="bilqassc.com"/>
<data android:scheme="https" android:host="bilqassc.com"/>
<data android:scheme="http" android:host="*.bilqassc.com"/>
<data android:scheme="https" android:host="*.bilqassc.com"/>
</intent-filter>
</activity>
<!-- ADS -->
<service
android:name=".service.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
Environment
- What version of the Android SDK are you using? 30
- How did you add the SDK to your project? maven