Skip to content

Commit 1bc796a

Browse files
committed
Fix notification deletion for pre-M
1 parent c784142 commit 1bc796a

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/UnityNotificationManager.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ protected static Notification buildNotificationForSending(Context context, Class
332332
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
333333
// Can't check StatusBar notifications pre-M, so ask to be notified when dismissed
334334
Intent deleteIntent = new Intent(context, UnityNotificationManager.class);
335+
deleteIntent.setAction(KEY_NOTIFICATION_DISMISSED); // need action to distinguish intent from content one
335336
deleteIntent.putExtra(KEY_NOTIFICATION_DISMISSED, id);
336337
PendingIntent deletePending = getBroadcastPendingIntent(context, id, deleteIntent, 0);
337338
builder.setDeleteIntent(deletePending);
@@ -661,6 +662,15 @@ public void cancelAllNotifications() {
661662
@Override
662663
public void onReceive(Context context, Intent intent) {
663664
try {
665+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
666+
if (KEY_NOTIFICATION_DISMISSED.equals(intent.getAction())) {
667+
int removedId = intent.getIntExtra(KEY_NOTIFICATION_DISMISSED, -1);
668+
if (removedId > 0) synchronized (UnityNotificationManager.class) {
669+
mVisibleNotifications.remove(removedId);
670+
}
671+
return;
672+
}
673+
}
664674
Object notification = getNotificationOrBuilderForIntent(context, intent);
665675
if (notification != null) {
666676
Notification notif = null;
@@ -695,12 +705,6 @@ public void onReceive(Context context, Intent intent) {
695705
UnityNotificationManager.notify(context, id, notif);
696706
}
697707
}
698-
else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
699-
int removedId = intent.getIntExtra(KEY_NOTIFICATION_DISMISSED, -1);
700-
if (removedId > 0) synchronized (UnityNotificationManager.class) {
701-
mVisibleNotifications.remove(removedId);
702-
}
703-
}
704708
} catch (BadParcelableException e) {
705709
Log.w(TAG_UNITY, e.toString());
706710
}
@@ -709,10 +713,9 @@ else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
709713
// Call the system notification service to notify the notification.
710714
protected static void notify(Context context, int id, Notification notification) {
711715
getNotificationManager(context).notify(id, notification);
712-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
713-
synchronized (UnityNotificationManager.class) {
714-
mVisibleNotifications.add(Integer.valueOf(id));
715-
}
716+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) synchronized (UnityNotificationManager.class) {
717+
mVisibleNotifications.add(Integer.valueOf(id));
718+
}
716719

717720
try {
718721
mNotificationCallback.onSentNotification(notification);

0 commit comments

Comments
 (0)