@@ -38,6 +38,7 @@ public class UnityNotificationManager extends BroadcastReceiver {
38
38
protected static NotificationCallback mNotificationCallback ;
39
39
protected static UnityNotificationManager mUnityNotificationManager ;
40
40
private static HashMap <Integer , Notification > mScheduledNotifications = new HashMap ();
41
+ private static HashSet <Integer > mVisibleNotifications = new HashSet <>();
41
42
private static int mSentSinceLastHousekeeping = 0 ;
42
43
private static boolean mPerformingHousekeeping = false ;
43
44
@@ -57,6 +58,7 @@ public class UnityNotificationManager extends BroadcastReceiver {
57
58
protected static final String KEY_NOTIFICATION_ID = "com.unity.NotificationID" ;
58
59
protected static final String KEY_SMALL_ICON = "smallIcon" ;
59
60
protected static final String KEY_CHANNEL_ID = "channelID" ;
61
+ protected static final String KEY_NOTIFICATION_DISMISSED = "com.unity.NotificationDismissed" ;
60
62
61
63
protected static final String NOTIFICATION_CHANNELS_SHARED_PREFS = "UNITY_NOTIFICATIONS" ;
62
64
protected static final String NOTIFICATION_CHANNELS_SHARED_PREFS_KEY = "ChannelIDs" ;
@@ -326,6 +328,15 @@ protected static Notification buildNotificationForSending(Context context, Class
326
328
openAppIntent .putExtra (KEY_NOTIFICATION_ID , id );
327
329
PendingIntent pendingIntent = getActivityPendingIntent (context , id , openAppIntent , 0 );
328
330
builder .setContentIntent (pendingIntent );
331
+
332
+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
333
+ // Can't check StatusBar notifications pre-M, so ask to be notified when dismissed
334
+ Intent deleteIntent = new Intent (context , UnityNotificationManager .class );
335
+ deleteIntent .putExtra (KEY_NOTIFICATION_DISMISSED , id );
336
+ PendingIntent deletePending = getBroadcastPendingIntent (context , id , deleteIntent , 0 );
337
+ builder .setDeleteIntent (deletePending );
338
+ }
339
+
329
340
finalizeNotificationForDisplay (context , builder );
330
341
return builder .build ();
331
342
}
@@ -437,6 +448,12 @@ private static Set<String> findInvalidNotificationIds(Context context, Set<Strin
437
448
invalid .remove (id );
438
449
}
439
450
}
451
+ else synchronized (UnityNotificationManager .class ) {
452
+ for (Integer visibleId : mVisibleNotifications ) {
453
+ String id = String .valueOf (visibleId );
454
+ invalid .remove (id );
455
+ }
456
+ }
440
457
441
458
// if app is launched with notification, user still has access to it
442
459
if (UnityPlayer .currentActivity != null ) {
@@ -548,18 +565,21 @@ protected static void scheduleNotificationIntentAlarm(Context context, long repe
548
565
// Check the notification status by id.
549
566
public int checkNotificationStatus (int id ) {
550
567
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
551
- // TODO: what if the notification has been dismissed by the user?
552
568
for (StatusBarNotification n : getNotificationManager ().getActiveNotifications ()) {
553
569
if (id == n .getId ())
554
570
return 2 ;
555
571
}
572
+ } else synchronized (UnityNotificationManager .class ) {
573
+ for (Integer notificationId : mVisibleNotifications ) {
574
+ if (notificationId .intValue () == id )
575
+ return 2 ;
576
+ }
577
+ }
556
578
557
- if (checkIfPendingNotificationIsRegistered (id ))
558
- return 1 ;
579
+ if (checkIfPendingNotificationIsRegistered (id ))
580
+ return 1 ;
559
581
560
- return 0 ;
561
- }
562
- return -1 ;
582
+ return 0 ;
563
583
}
564
584
565
585
// Check if the pending notification with the given id has been registered.
@@ -674,6 +694,12 @@ public void onReceive(Context context, Intent intent) {
674
694
UnityNotificationManager .notify (context , id , notif );
675
695
}
676
696
}
697
+ else if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
698
+ int removedId = intent .getIntExtra (KEY_NOTIFICATION_DISMISSED , -1 );
699
+ if (removedId > 0 ) synchronized (UnityNotificationManager .class ) {
700
+ mVisibleNotifications .remove (removedId );
701
+ }
702
+ }
677
703
} catch (BadParcelableException e ) {
678
704
Log .w (TAG_UNITY , e .toString ());
679
705
}
@@ -682,6 +708,10 @@ public void onReceive(Context context, Intent intent) {
682
708
// Call the system notification service to notify the notification.
683
709
protected static void notify (Context context , int id , Notification notification ) {
684
710
getNotificationManager (context ).notify (id , notification );
711
+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M )
712
+ synchronized (UnityNotificationManager .class ) {
713
+ mVisibleNotifications .add (Integer .valueOf (id ));
714
+ }
685
715
686
716
try {
687
717
mNotificationCallback .onSentNotification (notification );
0 commit comments