I have a notification with following content intent:
clickIntent = PendingIntent.getService(
getApplicationContext(),
Constants.REQUEST_CODE_NOTIFICATION_OTHER,
new Intent(getApplicationContext(), MainService.class)
.putExtra(Constants.EVENT, Constants.EVENT_TOGGLE_BLACK),
PendingIntent.FLAG_UPDATE_CURRENT);
and one action with following action intent:
PendingIntent closeIntent = PendingIntent.getService(
getApplicationContext(),
Constants.REQUEST_CODE_NOTIFICATION_OTHER,
new Intent(getApplicationContext(), MainService.class)
.putExtra(Constants.EVENT, Constants.EVENT_DISABLE_SERVICE),
PendingIntent.FLAG_UPDATE_CURRENT);
When I click the notification, the close intent is fired. Why?
Here is how I create the notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(text))
.setStyle(new NotificationCompat.BigTextStyle().bigText(getString(details)))
.setSmallIcon(R.drawable.ic_not)
.setContentIntent(clickIntent)
.setOngoing(true);
boolean showCloseButton = MainApp.getPrefs().enableCloseServiceButtonInNotification();
if (showCloseButton)
builder.addAction(R.drawable.ic_close_black_24dp, getString(R.string.stop_service), closeIntent);
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it.
In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.
Build and issue the notification: Create an Intent that starts the Activity . Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK . Create a PendingIntent by calling getActivity() .
FLAG_IMMUTABLE : Indicates the Intent inside the PendingIntent cannot be modified by other apps that pass an Intent to PendingIntent.send() . An app can always use FLAG_UPDATE_CURRENT to modify its own PendingIntents. Prior to Android 12, a PendingIntent created without this flag was mutable by default.
This is my guess... If does not work, I'll delete the answer...
I believe problem is that they are sharing same update number:
This is created:
clickIntent = PendingIntent.getService(
getApplicationContext(),
Constants.REQUEST_CODE_NOTIFICATION_OTHER,
new Intent(getApplicationContext(), MainService.class).putExtra(Constants.EVENT, Constants.EVENT_TOGGLE_BLACK),
PendingIntent.FLAG_UPDATE_CURRENT);
Then, this new one is created:
PendingIntent closeIntent = PendingIntent.getService(
getApplicationContext(),
Constants.REQUEST_CODE_NOTIFICATION_OTHER,
new Intent(getApplicationContext(), MainService.class).putExtra(Constants.EVENT, Constants.EVENT_DISABLE_SERVICE),
PendingIntent.FLAG_UPDATE_CURRENT);
It is similar to previous one and it is set PendingIntent.FLAG_UPDATE_CURRENT
.
So, the second one UPDATES the first one.
Try to use a different code for different PendingIntents
. Both are using Constants.REQUEST_CODE_NOTIFICATION_OTHER
.
Then, Android will use that code to differentiate them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With