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?