0

I am using onesignal sdk to start activity on notification click:

public class MainActivity extends AppCompatActivity {

 // adding webview variable
 private WebView mWebView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  // OneSignal Initialization
  OneSignal.startInit(this)
   .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
   .unsubscribeWhenNotificationsAreDisabled(true)
   .init();

  // initializing webview by matching with id in activity_main.xml
  mWebView = (WebView) findViewById(R.id.activity_main_webview);

  // Enable Javascript
  WebSettings webSettings = mWebView.getSettings();
  webSettings.setJavaScriptEnabled(true);


  // Setting up webview & webchrome client
  mWebView.setWebViewClient(new WebViewClient());
  mWebView.setWebChromeClient(new WebChromeClient());

  // if url is passed via onesignal notification as via putextra, open that url else open homepage
  String url = getIntent().getStringExtra("url");
  if (url != null) {
   mWebView.loadUrl(url);
  } else {
   mWebView.loadUrl("https://www.google.com");
  }


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

    JSONObject data = result.notification.payload.additionalData;
    String launchURL;

    if (data != null) {
     launchURL = data.optString("launchURL", null);

     //   Log.d("Debug", "Launch URL: " + launchURL);

     Intent intent = new Intent(getApplicationContext(), MainActivity.class);

     intent.putExtra("url", launchURL);

     startActivity(intent);


    } else {
     Log.d("Debug", result.notification.payload.toJSONObject().toString());

    }
   }
  }).init();


 }

}

On clicking notification, Mainactivity of my app starts and then the activity initiated by code startActivity(intent) with extra data put using intent.putExtra("url", launchURL) starts, causing 2 instances.

I have tried adding FLAG_ACTIVITY_REORDER_TO_FRONT & FLAG_ACTIVITY_CLEAR_TOP flags but they cause only the initial main activity to start, the activity initiated by startActivity(intent) is not started with these flags.

How to make only 1 instance of activity to start (the one which is initiated by startActivity(intent)) on notification click?

dc09
  • 386
  • 2
  • 17
  • I'm not getting why `startActivity(intent)` opens two instances of same activity. – azizbekian Apr 24 '20 at 15:00
  • I have updated the code with complete `MainActivity` class for better understanding. I think the default action on clicking the notification is opening the app and then `startActivity(intent)` is run via `notificationOpened` method. – dc09 Apr 24 '20 at 15:31
  • How do you know that there are two instances of your `MainActivity`? – Code-Apprentice Apr 24 '20 at 15:40
  • What about [this](https://stackoverflow.com/questions/19039189/intent-if-activity-is-running-bring-it-to-front-else-start-a-new-one-from-n)? – Code-Apprentice Apr 24 '20 at 15:43
  • 2 reasons: 1. App webview activity twice on phone when notification is clicked, once with the custom url passed via notification and once with the default url. Default url always opens before the custom url. 2. on clicking the notification, both the messages are logged - `if (url != null) { mWebView.loadUrl(url); Log.d("customUrl",url);)} else { mWebView.loadUrl("https://www.google.com"); Log.d("defaultUrl","Default"); }` First the default `defaultUrl` message is logged and then the `customUrl` message is logged. – dc09 Apr 24 '20 at 15:47
  • Adding `android:launchMode="singleTask"` in manifest causes the default url to open, url passed `intent.putExtra("url", launchURL);` is null. – dc09 Apr 24 '20 at 15:54
  • 1
    Instead of performing `startActivity(intent)` just perform `mWebView.loadUrl("some url")`, I can see no specific need for calling `startActivity()` – azizbekian Apr 24 '20 at 16:59
  • Using `mWebView.loadUrl("some url")` worked. – dc09 Apr 24 '20 at 20:38

0 Answers0