Skip to content

Commit cd5d292

Browse files
committed
Specialize notification enqueue
1 parent 4d37f27 commit cd5d292

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
package com.unity.androidnotifications;
22

3+
import android.app.Notification;
34
import android.content.Context;
45

56
import java.util.concurrent.LinkedTransferQueue;
67
import java.util.Set;
78

89
public class UnityNotificationBackgroundThread extends Thread {
10+
public static class ScheduleNotificationTask implements Runnable {
11+
private Notification.Builder notificationBuilder;
12+
13+
public ScheduleNotificationTask(Notification.Builder builder) {
14+
notificationBuilder = builder;
15+
}
16+
17+
@Override
18+
public void run() {
19+
UnityNotificationManager.mUnityNotificationManager.performNotificationScheduling(notificationBuilder);
20+
}
21+
}
22+
923
private LinkedTransferQueue<Runnable> mTasks = new LinkedTransferQueue();
1024

1125
public UnityNotificationBackgroundThread() {
1226
enqueueHousekeeping();
1327
}
1428

15-
public void enqueueTask(Runnable task) {
29+
public void enqueueNotification(ScheduleNotificationTask task) {
1630
mTasks.add(task);
1731
}
1832

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -261,35 +261,38 @@ public Object[] getNotificationChannels() {
261261
public void scheduleNotification(Notification.Builder notificationBuilder) {
262262
int id = notificationBuilder.getExtras().getInt(KEY_ID, -1);
263263
putScheduledNotification(id);
264-
mBackgroundThread.enqueueTask(() -> {
265-
Bundle extras = notificationBuilder.getExtras();
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.enqueueNotification(new UnityNotificationBackgroundThread.ScheduleNotificationTask(notificationBuilder));
265+
}
277266

278-
Intent intent = buildNotificationIntentUpdateList(mContext, id);
267+
protected void performNotificationScheduling(Notification.Builder notificationBuilder) {
268+
Bundle extras = notificationBuilder.getExtras();
269+
int id = extras.getInt(KEY_ID, -1);
270+
long repeatInterval = extras.getLong(KEY_REPEAT_INTERVAL, -1);
271+
long fireTime = extras.getLong(KEY_FIRE_TIME, -1);
272+
Notification notification = null;
279273

280-
if (intent != null) {
281-
UnityNotificationManager.saveNotification(mContext, notificationBuilder.build());
282-
notification = scheduleAlarmWithNotification(notificationBuilder, intent, fireTime);
283-
}
274+
// if less than a second in the future, notify right away
275+
boolean fireNow = fireTime - Calendar.getInstance().getTime().getTime() < 1000;
276+
if (!fireNow || repeatInterval > 0) {
277+
if (fireNow) {
278+
// schedule at next repetition
279+
fireTime += repeatInterval;
284280
}
285281

286-
if (fireNow) {
287-
if (notification == null) {
288-
notification = buildNotificationForSending(mContext, mOpenActivity, notificationBuilder);
289-
}
290-
notify(mContext, id, notification);
282+
Intent intent = buildNotificationIntentUpdateList(mContext, id);
283+
284+
if (intent != null) {
285+
UnityNotificationManager.saveNotification(mContext, notificationBuilder.build());
286+
notification = scheduleAlarmWithNotification(notificationBuilder, intent, fireTime);
291287
}
292-
});
288+
}
289+
290+
if (fireNow) {
291+
if (notification == null) {
292+
notification = buildNotificationForSending(mContext, mOpenActivity, notificationBuilder);
293+
}
294+
notify(mContext, id, notification);
295+
}
293296
}
294297

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

0 commit comments

Comments
 (0)