I have an app that sends notifications, and when clicked, opens an activity from my app. If you launch the app and keep the app open and click notifications, it works properly. However, when the app is closed, it doesn't. This is what happens:
Close app > receive notification > click notification > opens the correct activity > receive another notification > nothing happens when clicked
The intended functionality is for the notification to open the activity regardless if it is already open, and just add it onto the task stack. So if you clicked 3 notifications, the stack would be ActivityA > ActivityA > ActivityA. Here is the code for the notification:
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(ID, new NotificationCompat.Builder(context, CHANNEL_ID)
.setOnlyAlertOnce(false)
.setAutoCancel(true)
.setCustomContentView(helper.createSmallView(context))
.setCustomBigContentView(helper.createBigView(context))
.setContentIntent(PendingIntent.getActivity(context, 1,
new Intent(context, ActivityA.class),
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE)
)
.build());
I also tried using a broadcast PendingIntent
. The BroadcastReceiver
would be called but the call to Context#startActivity
did nothing.
CodePudding user response:
Intent intent = new Intent(YourActivity.class, NewActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
PendingIntent
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(ID, new NotificationCompat.Builder(context, CHANNEL_ID)
.setOnlyAlertOnce(false)
.setAutoCancel(true)
.setCustomContentView(helper.createSmallView(context))
.setCustomBigContentView(helper.createBigView(context))
.setContentIntent(pendingIntent)
.build());
CodePudding user response:
You have to use pending intent.