Skip to content

Commit ebaa8d1

Browse files
committed
Merge branch 'master' into rework-serialization
2 parents e8ae0c3 + abc3504 commit ebaa8d1

File tree

14 files changed

+364
-261
lines changed

14 files changed

+364
-261
lines changed

TestProjects/Main/Assets/Resources/iOSNotifications/TimeIntervalTrigger/Only Body.asset

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ MonoBehaviour:
1212
m_Script: {fileID: 11500000, guid: 4fc6c74f6df364b36b9afcbfd8cfbca3, type: 3}
1313
m_Name: Only Body
1414
m_EditorClassIdentifier:
15-
ButtonName: Empty notification + body
15+
ButtonName: Empty notification + body (silent)
1616
Identifier:
1717
CategoryIdentifier:
1818
ThreadIdentifier:
@@ -25,3 +25,4 @@ MonoBehaviour:
2525
Data:
2626
TimeTriggerInterval: 1
2727
Repeats: 0
28+
SoundType: 4

TestProjects/Main/Assets/Scripts/ScriptableObjects/iOSNotificationTemplateTimeTrigger.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class iOSNotificationTemplateTimeTrigger : ScriptableObject
3636
[Header("Time Trigger")]
3737
public Int32 TimeTriggerInterval;
3838
public bool Repeats = false;
39+
public NotificationSoundType SoundType;
3940

4041
[Serializable]
4142
public struct UserDataItem

TestProjects/Main/Assets/Scripts/iOSTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ private void InstantiateAllTestButtons()
226226
ForegroundPresentationOption = template.PresentationOptions,
227227
Badge = template.Badge,
228228
Data = template.Data,
229+
SoundType = template.SoundType,
229230
Trigger = new iOSNotificationTimeIntervalTrigger()
230231
{
231232
TimeInterval = TimeSpan.FromSeconds(template.TimeTriggerInterval),

com.unity.mobile.notifications/Documentation~/iOS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,9 @@ Sometimes, you might want to set custom data on the payload for a remote notific
225225
```
226226

227227
You have to use the exact `data` as the key of your custom data, because this is what the package looks for.
228+
229+
#### Images, videos and sounds
230+
231+
By default notifications use the default system sound. Sound can be disabled by changing the [iOSNotificationCenter.SoundType](../api/Unity.Notifications.iOS.iOSNotification.html#Unity_Notifications_iOS_iOSNotification_SoundType). A custom sound can be used by using Default sound type and assigning sound file name to [iOSNotificationCenter.SoundName](../api/Unity.Notifications.iOS.iOSNotification.html#Unity_Notifications_iOS_iOSNotification_SoundName). The sound file itself has to be manually added to XCode project. For more information regarding file placement and supported formats, see [Apple documentation](https://developer.apple.com/documentation/usernotifications/unnotificationsound?language=objc).
232+
233+
Images or video can added to notifications by using [attachments](../api/Unity.Notifications.iOS.iOSNotificationAttachment.html).

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 {
@@ -34,7 +33,7 @@ public ScheduleNotificationTask(int id, Notification.Builder builder, boolean cu
3433
}
3534

3635
@Override
37-
public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
36+
public boolean run(UnityNotificationManager manager, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
3837
String id = String.valueOf(notificationId);
3938
Integer ID = Integer.valueOf(notificationId);
4039
boolean didSchedule = false;
@@ -45,8 +44,8 @@ public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Buil
4544
// if failed to schedule or replace, remove
4645
if (!didSchedule) {
4746
notifications.remove(notificationId);
48-
UnityNotificationManager.cancelPendingNotificationIntent(context, notificationId);
49-
UnityNotificationManager.deleteExpiredNotificationIntent(context, id);
47+
manager.cancelPendingNotificationIntent(notificationId);
48+
manager.deleteExpiredNotificationIntent(id);
5049
}
5150
}
5251

@@ -62,10 +61,10 @@ public CancelNotificationTask(int id) {
6261
}
6362

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

@@ -75,15 +74,15 @@ public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Buil
7574

7675
private static class CancelAllNotificationsTask extends Task {
7776
@Override
78-
public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
77+
public boolean run(UnityNotificationManager manager, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
7978
if (notifications.isEmpty())
8079
return false;
8180

8281
Enumeration<Integer> ids = notifications.keys();
8382
while (ids.hasMoreElements()) {
8483
Integer notificationId = ids.nextElement();
85-
UnityNotificationManager.cancelPendingNotificationIntent(context, notificationId);
86-
UnityNotificationManager.deleteExpiredNotificationIntent(context, String.valueOf(notificationId));
84+
manager.cancelPendingNotificationIntent(notificationId);
85+
manager.deleteExpiredNotificationIntent(String.valueOf(notificationId));
8786
}
8887

8988
notifications.clear();
@@ -99,25 +98,25 @@ public HousekeepingTask(UnityNotificationBackgroundThread th) {
9998
}
10099

101100
@Override
102-
public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
101+
public boolean run(UnityNotificationManager manager, ConcurrentHashMap<Integer, Notification.Builder> notifications) {
103102
HashSet<String> notificationIds = new HashSet<>();
104103
Enumeration<Integer> ids = notifications.keys();
105104
while (ids.hasMoreElements()) {
106105
notificationIds.add(String.valueOf(ids.nextElement()));
107106
}
108-
thread.performHousekeeping(context, notificationIds);
107+
thread.performHousekeeping(notificationIds);
109108
return false;
110109
}
111110
}
112111

113112
private static final int TASKS_FOR_HOUSEKEEPING = 50;
114113
private LinkedTransferQueue<Task> mTasks = new LinkedTransferQueue();
115114
private ConcurrentHashMap<Integer, Notification.Builder> mScheduledNotifications;
116-
private static Context mContext;
115+
private UnityNotificationManager mManager;
117116
private int mTasksSinceHousekeeping = TASKS_FOR_HOUSEKEEPING; // we want hoursekeeping at the start
118117

119-
public UnityNotificationBackgroundThread(Context context, ConcurrentHashMap<Integer, Notification.Builder> scheduledNotifications) {
120-
mContext = context;
118+
public UnityNotificationBackgroundThread(UnityNotificationManager manager, ConcurrentHashMap<Integer, Notification.Builder> scheduledNotifications) {
119+
mManager = manager;
121120
mScheduledNotifications = scheduledNotifications;
122121
// rescheduling after reboot may have loaded, otherwise load here
123122
if (mScheduledNotifications.size() == 0)
@@ -146,7 +145,7 @@ public void run() {
146145
while (true) {
147146
try {
148147
Task task = mTasks.take();
149-
haveChanges |= executeTask(mContext, task, mScheduledNotifications);
148+
haveChanges |= executeTask(mManager, task, mScheduledNotifications);
150149
if (!(task instanceof HousekeepingTask))
151150
++mTasksSinceHousekeeping;
152151
if (mTasks.size() == 0 && haveChanges) {
@@ -160,26 +159,26 @@ public void run() {
160159
}
161160
}
162161

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

172-
private void performHousekeeping(Context context, Set<String> notificationIds) {
171+
private void performHousekeeping(Set<String> notificationIds) {
173172
// don't do housekeeping if last task we did was housekeeping (other=1)
174173
boolean performHousekeeping = mTasksSinceHousekeeping >= TASKS_FOR_HOUSEKEEPING;
175174
mTasksSinceHousekeeping = 0;
176175
if (performHousekeeping)
177-
UnityNotificationManager.performNotificationHousekeeping(context, notificationIds);
178-
UnityNotificationManager.saveScheduledNotificationIDs(context, notificationIds);
176+
mManager.performNotificationHousekeeping(notificationIds);
177+
mManager.saveScheduledNotificationIDs(notificationIds);
179178
}
180179

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

0 commit comments

Comments
 (0)