Recently we found an extra callback from our Notification.DeleteIntent when clicking on the Notification. Then we found out that it only happened on Android 11, not Android 9 or before (not sure about Android 10, do not have devices of it).
I.e.,
- For Android 9 and before,
Notification.ContentIntentbeen called when clicking on the Notification, andNotification.DeleteIntentbeen called when swiping out the Notification. - For Android 11,
Notification.ContentIntentbeen called when clicking on the Notification, andNotification.DeleteIntentbeen called when clicking on or swiping out the Notification.
Is it intended behavior of Android 11 platform? Or something is not correct in our codes? Thanks!
public class NotificationClickedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Note: Received callback when Notification been clicked.
}
}
public class NotificationCancelledReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Note: On Android 9 and before, received callback only when Notification been cancelled (swipe out).
// Note: On Android 11, received callback when Notification been cancelled or clicked.
}
}
{
...
Intent intent = new Intent(context, NotificationClickedReceiver.class);
PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent = new Intent(context, NotificationCancelledReceiver.class);
PendingIntent deletedIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder mBuilder = new Notification.Builder(context);
mBuilder.setContentTitle("title")
.setContentText("text")
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setDeleteIntent(deletedIntent)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
...
}