Skip to content

Commit 1e23869

Browse files
committed
Generate id on Java side when not provided
1 parent 6bcfa72 commit 1e23869

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

com.unity.mobile.notifications/Runtime/Android/AndroidNotificationCenter.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void CollectMethods(AndroidJavaClass clazz)
114114
setNotificationGroupAlertBehavior = JniApi.FindMethod(clazz, "setNotificationGroupAlertBehavior", "(Landroid/app/Notification$Builder;I)V", true);
115115
getNotificationGroupAlertBehavior = JniApi.FindMethod(clazz, "getNotificationGroupAlertBehavior", "(Landroid/app/Notification;)I", true);
116116
getNotificationChannelId = JniApi.FindMethod(clazz, "getNotificationChannelId", "(Landroid/app/Notification;)Ljava/lang/String;", true);
117-
scheduleNotification = JniApi.FindMethod(clazz, "scheduleNotification", "(Landroid/app/Notification$Builder;)V", false);
117+
scheduleNotification = JniApi.FindMethod(clazz, "scheduleNotification", "(Landroid/app/Notification$Builder;)I", false);
118118
createNotificationBuilder = JniApi.FindMethod(clazz, "createNotificationBuilder", "(Ljava/lang/String;)Landroid/app/Notification$Builder;", false);
119119
}
120120

@@ -195,9 +195,9 @@ public void DeleteNotificationChannel(string channelId)
195195
self.Call("deleteNotificationChannel", channelId);
196196
}
197197

198-
public void ScheduleNotification(AndroidJavaObject notificationBuilder)
198+
public int ScheduleNotification(AndroidJavaObject notificationBuilder)
199199
{
200-
self.Call(scheduleNotification, notificationBuilder);
200+
return self.Call<int>(scheduleNotification, notificationBuilder);
201201
}
202202

203203
public bool CheckIfPendingNotificationIsRegistered(int id)
@@ -860,8 +860,11 @@ public static void OpenNotificationSettings(string channelId = null)
860860
/// </summary>
861861
public static AndroidJavaObject CreateNotificationBuilder(AndroidNotification notification, string channelId)
862862
{
863-
int id = Math.Abs(DateTime.Now.ToString("yyMMddHHmmssffffff").GetHashCode()) + (new System.Random().Next(10000));
864-
return CreateNotificationBuilder(id, notification, channelId);
863+
AndroidJavaObject builder, extras;
864+
CreateNotificationBuilder(notification, channelId, out builder, out extras);
865+
if (extras != null)
866+
extras.Dispose();
867+
return builder;
865868
}
866869

867870
/// <summary>

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import java.lang.Integer;
3232
import java.util.Calendar;
33+
import java.util.Random;
3334
import java.util.Set;
3435
import java.util.HashSet;
3536
import java.util.ArrayList;
@@ -300,12 +301,30 @@ public NotificationChannelWrapper[] getNotificationChannels() {
300301
}
301302
}
302303

303-
// This is called from Unity managed code to call AlarmManager to set a broadcast intent for sending a notification.
304-
public void scheduleNotification(Notification.Builder notificationBuilder) {
305-
int id = notificationBuilder.getExtras().getInt(KEY_ID, -1);
304+
private int generateUniqueId() {
305+
int id = 0;
306+
Random random = new Random();
307+
do {
308+
id += random.nextInt(1000);
309+
} while (mScheduledNotifications.containsKey(Integer.valueOf(id)));
310+
311+
return id;
312+
}
313+
314+
public int scheduleNotification(Notification.Builder notificationBuilder) {
315+
Bundle extras = notificationBuilder.getExtras();
316+
int id;
317+
if (extras.containsKey(KEY_ID))
318+
id = notificationBuilder.getExtras().getInt(KEY_ID, -1);
319+
else {
320+
id = generateUniqueId();
321+
extras.putInt(KEY_ID, id);
322+
}
323+
306324
// don't replace existing, only put null as placeholder so we properly report status as being scheduled
307325
mScheduledNotifications.putIfAbsent(Integer.valueOf(id), null);
308326
mBackgroundThread.enqueueNotification(id, notificationBuilder);
327+
return id;
309328
}
310329

311330
protected void performNotificationScheduling(int id, Notification.Builder notificationBuilder) {

0 commit comments

Comments
 (0)