Android - open or restart app after clicking push notification using flag activities

邮差的信 提交于 2019-12-18 19:19:39

问题


I am using push notifications in Android. When I receive a push notification, I want to open the application if it is still running, else it should open a new instance of it.

I am using

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 
    Intent.FLAG_ACTIVITY_CLEAR_TOP | 
    Intent.FLAG_ACTIVITY_SINGLE_TOP | 
    Intent.FLAG_ACTIVITY_NEW_TASK);

But when I receive a push notification now and I click on it, nothing happens.

How can I still achieve this by using flagIntents?


回答1:


You need to set the Intent flags on the Intent. You were specifying them in the call to get a PendingIntent. Try this:

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
    Intent.FLAG_ACTIVITY_SINGLE_TOP | 
    Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
    notificationIntent, 0);



回答2:


set following things in your Android Mainfest file

android:noHistory="true"
 android:launchMode = "singleTop"



回答3:


In my case a running application restarted every time I clicked a push notification. But I don't want to restart the application.

In PendingIntent I have an intent for needed Activity1 that called Activity2. A bug was in these lines:

val intent = Intent(context, Activity2::class.java)
// Remove this line:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
context.startActivity(intent)


来源:https://stackoverflow.com/questions/13581574/android-open-or-restart-app-after-clicking-push-notification-using-flag-activi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!