简体   繁体   English

OneSignal | 从通知负载中获取URL并在自定义WebView中打开

[英]OneSignal | Get URL from notification payload and open in custom WebView

OneSignal provides functionality to open URLs in a browser by default. OneSignal提供了默认情况下在浏览器中打开URL的功能。 I want to send a URL with notifications that open the URL in a WebView that I have created. 我想发送一个带有通知的URL,该通知会在我创建的WebView中打开URL。 I've shared a snipper of where I receive the notification: 我分享了一条通知的摘要:

 class ExampleNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
      @Override
      public void notificationReceived(OSNotification notification) {
        JSONObject data = notification.payload.additionalData;
        String customKey;

        if (data != null) {
          customKey = data.optString("customkey", null);
          if (customKey != null)
            Log.i("OneSignalExample", "customkey set with value: " + customKey);
        }
      }
    }

I'm not sure how to retrieve the URL on the push being opened and open it in the WebView . 我不确定如何在打开的推入中检索URL并在WebView打开它。

The answer from SlashG, helped me. SlashG的回答对我有所帮助。 Here is my implementation... I added this code in my Application class onCreate() 这是我的实现...我在Application类onCreate()添加了此代码

OneSignal.startInit(this).setNotificationOpenedHandler(new OneSignal.NotificationOpenedHandler() {
            @Override
            public void notificationOpened(OSNotificationOpenResult result) {

                String launchURL = result.notification.payload.launchURL;

                if (launchURL != null) {
                    Log.d(Const.DEBUG, "Launch URL: " + launchURL);

                    Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("url", launchURL);
                    startActivity(intent);

                } else {
                    Log.d(Const.DEBUG, "Launch URL not found");

                    Intent intent = new Intent(getApplicationContext(), SplashActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        }).init();

If you're including the URL in your Additional Data , OneSignal will not do anything to use it. 如果您在其他数据中包含URL,OneSignal将不做任何使用。 Additional Data is for you to use as you may like to. 其他数据供您随意使用。 You need to implement a OneSignal.NotificationOpenedHandler and set it to your OneSignal instance when you call OneSignal.startInit(context) . 您需要实现一个OneSignal.NotificationOpenedHandler并将其设置为调用OneSignal.startInit(context) OneSignal实例。

This interface has a callback for when your notification is opened, notificationOpened() . 此接口有一个用于打开notificationOpened()的回调notificationOpened() This method has an argument of type OSNotificationOpenResult . 此方法的参数类型为OSNotificationOpenResult You can retrieve the notification > payload > additionalData . 您可以检索notification > payload > additionalData Get your launch_url from additionalData here and call a WebView with it. 让您的launch_urladditionalData这里调用WebView它。

Let me know if you need further assistance. 让我知道您是否需要进一步的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM