Balaji JB
Balaji JB

Reputation: 118

How to implement OneSignal push notification that opens URL in webview rather than browser?

How to open url in webview when onesignal push notification is send, right now it opens in the default browser and how to handle the target url by using notification handler. Here is the sample code where I want to implement onesignal notification and I tried my level best could not handle with it. Any suggestion from experts.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OneSignal.startInit(this).init();
}

handling

Upvotes: 3

Views: 9781

Answers (3)

Vamsi Challa
Vamsi Challa

Reputation: 11109

Here is my implementation... I added this code in my Application class 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();

Upvotes: 1

Claudio Shigueo Watanabe
Claudio Shigueo Watanabe

Reputation: 1003

One possibility is to create two types of messages, one to send to browsers and another one to the apps. This question has one example of how this can be done: https://wordpress.org/support/topic/variable-launchurl/

Not sure if it will help your case but I hope so.

Upvotes: 0

Gdeglin
Gdeglin

Reputation: 12618

In order to do this you should set your url as an additional data field instead of as a Launch URL.

Next, using the OneSignal Android NotificationReceivedHandler you can retrieve this additional data and open a webview to the URL.

Upvotes: 0

Related Questions