Skip to content

Commit 262d4af

Browse files
committed
Implement displayed notification system for pre-6 androids
1 parent 9ddcab4 commit 262d4af

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

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

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class UnityNotificationManager extends BroadcastReceiver {
3838
protected static NotificationCallback mNotificationCallback;
3939
protected static UnityNotificationManager mUnityNotificationManager;
4040
private static HashMap<Integer, Notification> mScheduledNotifications = new HashMap();
41+
private static HashSet<Integer> mVisibleNotifications = new HashSet<>();
4142
private static int mSentSinceLastHousekeeping = 0;
4243
private static boolean mPerformingHousekeeping = false;
4344

@@ -57,6 +58,7 @@ public class UnityNotificationManager extends BroadcastReceiver {
5758
protected static final String KEY_NOTIFICATION_ID = "com.unity.NotificationID";
5859
protected static final String KEY_SMALL_ICON = "smallIcon";
5960
protected static final String KEY_CHANNEL_ID = "channelID";
61+
protected static final String KEY_NOTIFICATION_DISMISSED = "com.unity.NotificationDismissed";
6062

6163
protected static final String NOTIFICATION_CHANNELS_SHARED_PREFS = "UNITY_NOTIFICATIONS";
6264
protected static final String NOTIFICATION_CHANNELS_SHARED_PREFS_KEY = "ChannelIDs";
@@ -326,6 +328,15 @@ protected static Notification buildNotificationForSending(Context context, Class
326328
openAppIntent.putExtra(KEY_NOTIFICATION_ID, id);
327329
PendingIntent pendingIntent = getActivityPendingIntent(context, id, openAppIntent, 0);
328330
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+
329340
finalizeNotificationForDisplay(context, builder);
330341
return builder.build();
331342
}
@@ -437,6 +448,12 @@ private static Set<String> findInvalidNotificationIds(Context context, Set<Strin
437448
invalid.remove(id);
438449
}
439450
}
451+
else synchronized (UnityNotificationManager.class) {
452+
for (Integer visibleId : mVisibleNotifications) {
453+
String id = String.valueOf(visibleId);
454+
invalid.remove(id);
455+
}
456+
}
440457

441458
// if app is launched with notification, user still has access to it
442459
if (UnityPlayer.currentActivity != null) {
@@ -548,18 +565,21 @@ protected static void scheduleNotificationIntentAlarm(Context context, long repe
548565
// Check the notification status by id.
549566
public int checkNotificationStatus(int id) {
550567
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
551-
// TODO: what if the notification has been dismissed by the user?
552568
for (StatusBarNotification n : getNotificationManager().getActiveNotifications()) {
553569
if (id == n.getId())
554570
return 2;
555571
}
572+
} else synchronized (UnityNotificationManager.class) {
573+
for (Integer notificationId : mVisibleNotifications) {
574+
if (notificationId.intValue() == id)
575+
return 2;
576+
}
577+
}
556578

557-
if (checkIfPendingNotificationIsRegistered(id))
558-
return 1;
579+
if (checkIfPendingNotificationIsRegistered(id))
580+
return 1;
559581

560-
return 0;
561-
}
562-
return -1;
582+
return 0;
563583
}
564584

565585
// Check if the pending notification with the given id has been registered.
@@ -674,6 +694,12 @@ public void onReceive(Context context, Intent intent) {
674694
UnityNotificationManager.notify(context, id, notif);
675695
}
676696
}
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+
}
677703
} catch (BadParcelableException e) {
678704
Log.w(TAG_UNITY, e.toString());
679705
}
@@ -682,6 +708,10 @@ public void onReceive(Context context, Intent intent) {
682708
// Call the system notification service to notify the notification.
683709
protected static void notify(Context context, int id, Notification notification) {
684710
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+
}
685715

686716
try {
687717
mNotificationCallback.onSentNotification(notification);

0 commit comments

Comments
 (0)