这是我的第一个Android / Java项目,遇到了我似乎无法解决的问题。
我的应用程序是一个Webview应用程序,在正常使用期间,它只会显示一个静态页面:example.com
。但是,有时我会发送带有指向其他页面链接的推送通知。通过以下代码,此功能可以正常使用...除非应用已打开。如果在应用已打开的情况下点击推送通知,则不会发生任何事情。
如何使ExampleNotificationOpenedHandler
用正确的URL重新加载MainActivity
?由于我仍然很陌生,因此可以复制和粘贴的代码将非常有帮助。
这是我的主要活动
protected void onCreate(Bundle savedInstanceState) {
// OneSignal Initialization
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
if(isNetworkStatusAvailable (getApplicationContext())) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WebView webView = (WebView) findViewById(R.id.web1);
webView.setWebViewClient(new WebViewClient());
if (ExampleNotificationOpenedHandler.launchURL == null){
webView.loadUrl("https://scripturememory.com/audioverse/app-home.php");
}else{
webView.loadUrl(ExampleNotificationOpenedHandler.launchURL);
}
这是我要在用户点击通知时进行处理的课程。
class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it and
// puts the launchURL additional data into a string for use in the main activity
public static String launchURL;
@Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
if (data != null) {
launchURL = data.optString("launchURL");
if (launchURL != null) {
Log.i("OneSignalExample", "launchURL value: " + launchURL);
}
}
}
}