Skip to content

Commit c9169bc

Browse files
committed
Perform scheduling on other thread
1 parent 12872ee commit c9169bc

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -261,34 +261,36 @@ public Object[] getNotificationChannels() {
261261

262262
// This is called from Unity managed code to call AlarmManager to set a broadcast intent for sending a notification.
263263
public void scheduleNotification(Notification.Builder notificationBuilder) {
264-
Bundle extras = notificationBuilder.getExtras();
265-
int id = extras.getInt(KEY_ID, -1);
266-
long repeatInterval = extras.getLong(KEY_REPEAT_INTERVAL, -1);
267-
long fireTime = extras.getLong(KEY_FIRE_TIME, -1);
268-
Notification notification = null;
269-
270-
// if less than a second in the future, notify right away
271-
boolean fireNow = fireTime - Calendar.getInstance().getTime().getTime() < 1000;
272-
if (!fireNow || repeatInterval > 0) {
273-
if (fireNow) {
274-
// schedule at next repetition
275-
fireTime += repeatInterval;
276-
}
264+
mBackgroundThread.enqueueTask(() -> {
265+
Bundle extras = notificationBuilder.getExtras();
266+
int id = extras.getInt(KEY_ID, -1);
267+
long repeatInterval = extras.getLong(KEY_REPEAT_INTERVAL, -1);
268+
long fireTime = extras.getLong(KEY_FIRE_TIME, -1);
269+
Notification notification = null;
270+
271+
// if less than a second in the future, notify right away
272+
boolean fireNow = fireTime - Calendar.getInstance().getTime().getTime() < 1000;
273+
if (!fireNow || repeatInterval > 0) {
274+
if (fireNow) {
275+
// schedule at next repetition
276+
fireTime += repeatInterval;
277+
}
277278

278-
Intent intent = buildNotificationIntentUpdateList(mContext, id);
279+
Intent intent = buildNotificationIntentUpdateList(mContext, id);
279280

280-
if (intent != null) {
281-
UnityNotificationManager.saveNotification(mContext, notificationBuilder.build());
282-
notification = scheduleAlarmWithNotification(notificationBuilder, intent, fireTime);
281+
if (intent != null) {
282+
UnityNotificationManager.saveNotification(mContext, notificationBuilder.build());
283+
notification = scheduleAlarmWithNotification(notificationBuilder, intent, fireTime);
284+
}
283285
}
284-
}
285286

286-
if (fireNow) {
287-
if (notification == null) {
288-
notification = buildNotificationForSending(mContext, mOpenActivity, notificationBuilder);
287+
if (fireNow) {
288+
if (notification == null) {
289+
notification = buildNotificationForSending(mContext, mOpenActivity, notificationBuilder);
290+
}
291+
notify(mContext, id, notification);
289292
}
290-
notify(mContext, id, notification);
291-
}
293+
});
292294
}
293295

294296
Notification scheduleAlarmWithNotification(Notification.Builder notificationBuilder, Intent intent, long fireTime) {

0 commit comments

Comments
 (0)