Skip to content

Commit abc3504

Browse files
authored
Merge pull request #216 from Unity-Technologies/refactor-notification-manager
Refactor notification manager
2 parents 42f2cd1 + d6a5321 commit abc3504

File tree

6 files changed

+230
-258
lines changed

6 files changed

+230
-258
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public NotificationManagerJni(AndroidJavaClass clazz, AndroidJavaObject obj)
106106

107107
void CollectMethods(AndroidJavaClass clazz)
108108
{
109-
getNotificationFromIntent = JniApi.FindMethod(clazz, "getNotificationFromIntent", "(Landroid/content/Context;Landroid/content/Intent;)Landroid/app/Notification;", true);
109+
getNotificationFromIntent = JniApi.FindMethod(clazz, "getNotificationFromIntent", "(Landroid/content/Intent;)Landroid/app/Notification;", false);
110110
setNotificationIcon = JniApi.FindMethod(clazz, "setNotificationIcon", "(Landroid/app/Notification$Builder;Ljava/lang/String;Ljava/lang/String;)V", true);
111111
setNotificationColor = JniApi.FindMethod(clazz, "setNotificationColor", "(Landroid/app/Notification$Builder;I)V", true);
112112
getNotificationColor = JniApi.FindMethod(clazz, "getNotificationColor", "(Landroid/app/Notification;)Ljava/lang/Integer;", true);
@@ -118,9 +118,9 @@ void CollectMethods(AndroidJavaClass clazz)
118118
createNotificationBuilder = JniApi.FindMethod(clazz, "createNotificationBuilder", "(Ljava/lang/String;)Landroid/app/Notification$Builder;", false);
119119
}
120120

121-
public AndroidJavaObject GetNotificationFromIntent(AndroidJavaObject activity, AndroidJavaObject intent)
121+
public AndroidJavaObject GetNotificationFromIntent(AndroidJavaObject intent)
122122
{
123-
return klass.CallStatic<AndroidJavaObject>(getNotificationFromIntent, activity, intent);
123+
return self.Call<AndroidJavaObject>(getNotificationFromIntent, intent);
124124
}
125125

126126
public void SetNotificationIcon(AndroidJavaObject builder, AndroidJavaObject keyName, string icon)
@@ -568,13 +568,11 @@ public static bool Initialize()
568568
#if UNITY_EDITOR || !UNITY_ANDROID
569569
s_CurrentActivity = null;
570570
#elif UNITY_ANDROID
571-
var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
572-
s_CurrentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
573-
var context = s_CurrentActivity.Call<AndroidJavaObject>("getApplicationContext");
571+
using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
572+
s_CurrentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
574573

575574
var notificationManagerClass = new AndroidJavaClass("com.unity.androidnotifications.UnityNotificationManager");
576-
var notificationManager = notificationManagerClass.CallStatic<AndroidJavaObject>("getNotificationManagerImpl", context, s_CurrentActivity);
577-
notificationManager.Call("setNotificationCallback", new NotificationCallback());
575+
var notificationManager = notificationManagerClass.CallStatic<AndroidJavaObject>("getNotificationManagerImpl", s_CurrentActivity, new NotificationCallback());
578576
s_Jni = new JniApi(notificationManagerClass, notificationManager);
579577

580578
s_Initialized = true;
@@ -841,7 +839,7 @@ public static AndroidNotificationIntentData GetLastNotificationIntent()
841839
return null;
842840

843841
var intent = s_CurrentActivity.Call<AndroidJavaObject>("getIntent");
844-
var notification = s_Jni.NotificationManager.GetNotificationFromIntent(s_CurrentActivity, intent);
842+
var notification = s_Jni.NotificationManager.GetNotificationFromIntent(intent);
845843
if (notification == null)
846844
return null;
847845
return GetNotificationData(notification);

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static com.unity.androidnotifications.UnityNotificationManager.TAG_UNITY;
55

66
import android.app.Notification;
7-
import android.content.Context;
87
import android.util.Log;
98

109
import java.util.concurrent.ConcurrentHashMap;
@@ -17,7 +16,7 @@
1716
public class UnityNotificationBackgroundThread extends Thread {
1817
private static abstract class Task {
1918
// returns true if notificationIds was modified (needs to be saved)
20-
public abstract boolean run(Context context, ConcurrentHashMap<Integer, Notification.Builder> notifications);
19+
public abstract boolean run(UnityNotificationManager manager, ConcurrentHashMap<Integer, Notification.Builder> notifications);
2120
}
2221

2322
private static class ScheduleNotificationTask extends Task {
@@ -32,7 +31,7 @@ public ScheduleNotificationTask(int id, Notification.Builder builder, boolean ad
3231
}
3332

3433
@Override
35-
public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
34+
public boolean run(UnityNotificationManager manager, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
3635
String id = String.valueOf(notificationId);
3736
Integer ID = Integer.valueOf(notificationId);
3837
boolean didSchedule = false;
@@ -43,8 +42,8 @@ public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Buil
4342
// if failed to schedule or replace, remove
4443
if (!didSchedule) {
4544
notifications.remove(notificationId);
46-
UnityNotificationManager.cancelPendingNotificationIntent(context, notificationId);
47-
UnityNotificationManager.deleteExpiredNotificationIntent(context, id);
45+
manager.cancelPendingNotificationIntent(notificationId);
46+
manager.deleteExpiredNotificationIntent(id);
4847
}
4948
}
5049

@@ -60,10 +59,10 @@ public CancelNotificationTask(int id) {
6059
}
6160

6261
@Override
63-
public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
64-
UnityNotificationManager.cancelPendingNotificationIntent(context, notificationId);
62+
public boolean run(UnityNotificationManager manager, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
63+
manager.cancelPendingNotificationIntent(notificationId);
6564
if (notifications.remove(notificationId) != null) {
66-
UnityNotificationManager.deleteExpiredNotificationIntent(context, String.valueOf(notificationId));
65+
manager.deleteExpiredNotificationIntent(String.valueOf(notificationId));
6766
return true;
6867
}
6968

@@ -73,15 +72,15 @@ public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Buil
7372

7473
private static class CancelAllNotificationsTask extends Task {
7574
@Override
76-
public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
75+
public boolean run(UnityNotificationManager manager, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
7776
if (notifications.isEmpty())
7877
return false;
7978

8079
Enumeration<Integer> ids = notifications.keys();
8180
while (ids.hasMoreElements()) {
8281
Integer notificationId = ids.nextElement();
83-
UnityNotificationManager.cancelPendingNotificationIntent(context, notificationId);
84-
UnityNotificationManager.deleteExpiredNotificationIntent(context, String.valueOf(notificationId));
82+
manager.cancelPendingNotificationIntent(notificationId);
83+
manager.deleteExpiredNotificationIntent(String.valueOf(notificationId));
8584
}
8685

8786
notifications.clear();
@@ -97,25 +96,25 @@ public HousekeepingTask(UnityNotificationBackgroundThread th) {
9796
}
9897

9998
@Override
100-
public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
99+
public boolean run(UnityNotificationManager manager, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
101100
HashSet<String> notificationIds = new HashSet<>();
102101
Enumeration<Integer> ids = notifications.keys();
103102
while (ids.hasMoreElements()) {
104103
notificationIds.add(String.valueOf(ids.nextElement()));
105104
}
106-
thread.performHousekeeping(context, notificationIds);
105+
thread.performHousekeeping(notificationIds);
107106
return false;
108107
}
109108
}
110109

111110
private static final int TASKS_FOR_HOUSEKEEPING = 50;
112111
private LinkedTransferQueue<Task> mTasks = new LinkedTransferQueue();
113112
private ConcurrentHashMap<Integer, Notification.Builder> mScheduledNotifications;
114-
private static Context mContext;
113+
private UnityNotificationManager mManager;
115114
private int mTasksSinceHousekeeping = TASKS_FOR_HOUSEKEEPING; // we want hoursekeeping at the start
116115

117-
public UnityNotificationBackgroundThread(Context context, ConcurrentHashMap<Integer, Notification.Builder> scheduledNotifications) {
118-
mContext = context;
116+
public UnityNotificationBackgroundThread(UnityNotificationManager manager, ConcurrentHashMap<Integer, Notification.Builder> scheduledNotifications) {
117+
mManager = manager;
119118
mScheduledNotifications = scheduledNotifications;
120119
// rescheduling after reboot may have loaded, otherwise load here
121120
if (mScheduledNotifications.size() == 0)
@@ -144,7 +143,7 @@ public void run() {
144143
while (true) {
145144
try {
146145
Task task = mTasks.take();
147-
haveChanges |= executeTask(mContext, task, mScheduledNotifications);
146+
haveChanges |= executeTask(mManager, task, mScheduledNotifications);
148147
if (!(task instanceof HousekeepingTask))
149148
++mTasksSinceHousekeeping;
150149
if (mTasks.size() == 0 && haveChanges) {
@@ -158,26 +157,26 @@ public void run() {
158157
}
159158
}
160159

161-
private boolean executeTask(Context context, Task task, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
160+
private boolean executeTask(UnityNotificationManager manager, Task task, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
162161
try {
163-
return task.run(context, notifications);
162+
return task.run(manager, notifications);
164163
} catch (Exception e) {
165164
Log.e(TAG_UNITY, "Exception executing notification task", e);
166165
return false;
167166
}
168167
}
169168

170-
private void performHousekeeping(Context context, Set<String> notificationIds) {
169+
private void performHousekeeping(Set<String> notificationIds) {
171170
// don't do housekeeping if last task we did was housekeeping (other=1)
172171
boolean performHousekeeping = mTasksSinceHousekeeping >= TASKS_FOR_HOUSEKEEPING;
173172
mTasksSinceHousekeeping = 0;
174173
if (performHousekeeping)
175-
UnityNotificationManager.performNotificationHousekeeping(context, notificationIds);
176-
UnityNotificationManager.saveScheduledNotificationIDs(context, notificationIds);
174+
mManager.performNotificationHousekeeping(notificationIds);
175+
mManager.saveScheduledNotificationIDs(notificationIds);
177176
}
178177

179178
private void loadNotifications() {
180-
List<Notification.Builder> notifications = UnityNotificationManager.loadSavedNotifications(mContext);
179+
List<Notification.Builder> notifications = mManager.loadSavedNotifications();
181180
for (Notification.Builder builder : notifications) {
182181
int id = builder.getExtras().getInt(KEY_ID, -1);
183182
mScheduledNotifications.put(id, builder);

0 commit comments

Comments
 (0)