Skip to content

Commit 2fe5e95

Browse files
authored
Merge pull request #197 from Unity-Technologies/merge-manager-classes
Merge manager classes
2 parents 68af9fb + bbe193f commit 2fe5e95

File tree

9 files changed

+163
-271
lines changed

9 files changed

+163
-271
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ public static AndroidNotificationChannel[] GetNotificationChannels()
637637
var androidChannels = s_Jni.NotificationManager.GetNotificationChannels();
638638
var channels = new AndroidNotificationChannel[androidChannels == null ? 0 : androidChannels.Length];
639639

640-
for (int i = 0; i < androidChannels.Length; ++i)
640+
for (int i = 0; i < channels.Length; ++i)
641641
{
642642
var channel = androidChannels[i];
643643
var ch = new AndroidNotificationChannel();

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

Lines changed: 0 additions & 7 deletions
This file was deleted.

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/NotificationCallback.java.meta

Lines changed: 0 additions & 32 deletions
This file was deleted.

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

Lines changed: 0 additions & 17 deletions
This file was deleted.

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/NotificationChannelWrapper.java.meta

Lines changed: 0 additions & 32 deletions
This file was deleted.

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

Lines changed: 121 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.unity.androidnotifications;
22

3+
import android.annotation.TargetApi;
34
import android.app.Activity;
45
import android.app.ActivityManager;
56
import android.app.AlarmManager;
67
import android.app.Notification;
8+
import android.app.NotificationChannel;
79
import android.app.NotificationManager;
810
import android.app.PendingIntent;
911
import android.content.BroadcastReceiver;
@@ -112,14 +114,8 @@ public static UnityNotificationManager getNotificationManagerImpl(Context contex
112114

113115
// Called from managed code.
114116
public static UnityNotificationManager getNotificationManagerImpl(Context context, Activity activity) {
115-
if (mUnityNotificationManager != null)
116-
return mUnityNotificationManager;
117-
118-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
119-
mUnityNotificationManager = new UnityNotificationManagerOreo(context, activity);
120-
} else {
117+
if (mUnityNotificationManager == null)
121118
mUnityNotificationManager = new UnityNotificationManager(context, activity);
122-
}
123119

124120
return mUnityNotificationManager;
125121
}
@@ -138,8 +134,6 @@ public void setNotificationCallback(NotificationCallback notificationCallback) {
138134
UnityNotificationManager.mNotificationCallback = notificationCallback;
139135
}
140136

141-
// Register a new notification channel.
142-
// This function will only be called for devices which are low than Android O.
143137
public void registerNotificationChannel(
144138
String id,
145139
String name,
@@ -151,42 +145,56 @@ public void registerNotificationChannel(
151145
boolean canShowBadge,
152146
long[] vibrationPattern,
153147
int lockscreenVisibility) {
154-
SharedPreferences prefs = mContext.getSharedPreferences(NOTIFICATION_CHANNELS_SHARED_PREFS, Context.MODE_PRIVATE);
155-
Set<String> channelIds = new HashSet<String>(prefs.getStringSet(NOTIFICATION_CHANNELS_SHARED_PREFS_KEY, new HashSet<String>()));
156-
channelIds.add(id); // TODO: what if users create the channel again with the same id?
148+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
149+
NotificationChannel channel = new NotificationChannel(id, name, importance);
150+
channel.setDescription(description);
151+
channel.enableLights(enableLights);
152+
channel.enableVibration(enableVibration);
153+
channel.setBypassDnd(canBypassDnd);
154+
channel.setShowBadge(canShowBadge);
155+
channel.setVibrationPattern(vibrationPattern);
156+
channel.setLockscreenVisibility(lockscreenVisibility);
157+
158+
getNotificationManager().createNotificationChannel(channel);
159+
} else {
160+
SharedPreferences prefs = mContext.getSharedPreferences(NOTIFICATION_CHANNELS_SHARED_PREFS, Context.MODE_PRIVATE);
161+
Set<String> channelIds = new HashSet<String>(prefs.getStringSet(NOTIFICATION_CHANNELS_SHARED_PREFS_KEY, new HashSet<String>()));
162+
channelIds.add(id); // TODO: what if users create the channel again with the same id?
157163

158-
// Add to notification channel ids SharedPreferences.
159-
SharedPreferences.Editor editor = prefs.edit().clear();
160-
editor.putStringSet("ChannelIDs", channelIds);
161-
editor.apply();
164+
// Add to notification channel ids SharedPreferences.
165+
SharedPreferences.Editor editor = prefs.edit().clear();
166+
editor.putStringSet("ChannelIDs", channelIds);
167+
editor.apply();
162168

163-
// Store the channel into a SharedPreferences.
164-
SharedPreferences channelPrefs = mContext.getSharedPreferences(getSharedPrefsNameByChannelId(id), Context.MODE_PRIVATE);
165-
editor = channelPrefs.edit();
169+
// Store the channel into a SharedPreferences.
170+
SharedPreferences channelPrefs = mContext.getSharedPreferences(getSharedPrefsNameByChannelId(id), Context.MODE_PRIVATE);
171+
editor = channelPrefs.edit();
166172

167-
editor.putString("title", name); // Sadly I can't change the "title" here to "name" due to backward compatibility.
168-
editor.putInt("importance", importance);
169-
editor.putString("description", description);
170-
editor.putBoolean("enableLights", enableLights);
171-
editor.putBoolean("enableVibration", enableVibration);
172-
editor.putBoolean("canBypassDnd", canBypassDnd);
173-
editor.putBoolean("canShowBadge", canShowBadge);
174-
editor.putString("vibrationPattern", Arrays.toString(vibrationPattern));
175-
editor.putInt("lockscreenVisibility", lockscreenVisibility);
173+
editor.putString("title", name); // Sadly I can't change the "title" here to "name" due to backward compatibility.
174+
editor.putInt("importance", importance);
175+
editor.putString("description", description);
176+
editor.putBoolean("enableLights", enableLights);
177+
editor.putBoolean("enableVibration", enableVibration);
178+
editor.putBoolean("canBypassDnd", canBypassDnd);
179+
editor.putBoolean("canShowBadge", canShowBadge);
180+
editor.putString("vibrationPattern", Arrays.toString(vibrationPattern));
181+
editor.putInt("lockscreenVisibility", lockscreenVisibility);
176182

177-
editor.apply();
183+
editor.apply();
184+
}
178185
}
179186

180187
protected static String getSharedPrefsNameByChannelId(String id)
181188
{
182189
return String.format("unity_notification_channel_%s", id);
183190
}
184191

185-
// Get a notification channel by id.
186-
// This function will only be called for devices which are low than Android O.
187-
protected static NotificationChannelWrapper getNotificationChannel(Context context, String id) {
192+
private static NotificationChannelWrapper getNotificationChannel(Context context, String id) {
188193
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
189-
return UnityNotificationManagerOreo.getOreoNotificationChannel(context, id);
194+
NotificationChannel ch = getNotificationManager(context).getNotificationChannel(id);
195+
if (ch == null)
196+
return null;
197+
return notificationChannelToWrapper(ch);
190198
}
191199

192200
SharedPreferences prefs = context.getSharedPreferences(getSharedPrefsNameByChannelId(id), Context.MODE_PRIVATE);
@@ -219,44 +227,77 @@ protected static NotificationChannelWrapper getNotificationChannel(Context conte
219227
return channel;
220228
}
221229

222-
// Get a notification channel by id.
223-
// This function will only be called for devices which are low than Android O.
224-
protected NotificationChannelWrapper getNotificationChannel(String id) {
230+
@TargetApi(Build.VERSION_CODES.O)
231+
private static NotificationChannelWrapper notificationChannelToWrapper(Object chan) {
232+
// Possibly unavailable classes cannot be in API, breaks reflection code looping over when searching for method
233+
NotificationChannel channel = (NotificationChannel)chan;
234+
NotificationChannelWrapper wrapper = new NotificationChannelWrapper();
235+
236+
wrapper.id = channel.getId();
237+
wrapper.name = channel.getName().toString();
238+
wrapper.importance = channel.getImportance();
239+
wrapper.description = channel.getDescription();
240+
wrapper.enableLights = channel.shouldShowLights();
241+
wrapper.enableVibration = channel.shouldVibrate();
242+
wrapper.canBypassDnd = channel.canBypassDnd();
243+
wrapper.canShowBadge = channel.canShowBadge();
244+
wrapper.vibrationPattern = channel.getVibrationPattern();
245+
wrapper.lockscreenVisibility = channel.getLockscreenVisibility();
246+
247+
return wrapper;
248+
}
249+
250+
public NotificationChannelWrapper getNotificationChannel(String id) {
225251
return UnityNotificationManager.getNotificationChannel(mContext, id);
226252
}
227253

228-
// Delete a notification channel by id.
229-
// This function will only be called for devices which are low than Android O.
230254
public void deleteNotificationChannel(String id) {
231-
SharedPreferences prefs = mContext.getSharedPreferences(NOTIFICATION_CHANNELS_SHARED_PREFS, Context.MODE_PRIVATE);
232-
Set<String> channelIds = new HashSet<String>(prefs.getStringSet(NOTIFICATION_CHANNELS_SHARED_PREFS_KEY, new HashSet<String>()));
255+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
256+
getNotificationManager().deleteNotificationChannel(id);
257+
} else {
258+
SharedPreferences prefs = mContext.getSharedPreferences(NOTIFICATION_CHANNELS_SHARED_PREFS, Context.MODE_PRIVATE);
259+
Set<String> channelIds = prefs.getStringSet(NOTIFICATION_CHANNELS_SHARED_PREFS_KEY, new HashSet());
233260

234-
if (!channelIds.contains(id))
235-
return;
261+
if (!channelIds.contains(id))
262+
return;
236263

237-
// Remove from the notification channel ids SharedPreferences.
238-
channelIds.remove(id);
239-
SharedPreferences.Editor editor = prefs.edit().clear();
240-
editor.putStringSet(NOTIFICATION_CHANNELS_SHARED_PREFS_KEY, channelIds);
241-
editor.apply();
264+
// Remove from the notification channel ids SharedPreferences.
265+
channelIds = new HashSet(channelIds);
266+
channelIds.remove(id);
267+
SharedPreferences.Editor editor = prefs.edit().clear();
268+
editor.putStringSet(NOTIFICATION_CHANNELS_SHARED_PREFS_KEY, channelIds);
269+
editor.apply();
242270

243-
// Delete the notification channel SharedPreferences.
244-
SharedPreferences channelPrefs = mContext.getSharedPreferences(getSharedPrefsNameByChannelId(id), Context.MODE_PRIVATE);
245-
channelPrefs.edit().clear().apply();
271+
// Delete the notification channel SharedPreferences.
272+
SharedPreferences channelPrefs = mContext.getSharedPreferences(getSharedPrefsNameByChannelId(id), Context.MODE_PRIVATE);
273+
channelPrefs.edit().clear().apply();
274+
}
246275
}
247276

248-
// Get all notification channels.
249-
// This function will only be called for devices which are low than Android O.
250-
public Object[] getNotificationChannels() {
251-
SharedPreferences prefs = mContext.getSharedPreferences(NOTIFICATION_CHANNELS_SHARED_PREFS, Context.MODE_PRIVATE);
252-
Set<String> channelIdsSet = prefs.getStringSet(NOTIFICATION_CHANNELS_SHARED_PREFS_KEY, new HashSet<String>());
253-
254-
ArrayList<NotificationChannelWrapper> channels = new ArrayList<>();
277+
public NotificationChannelWrapper[] getNotificationChannels() {
278+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
279+
List<NotificationChannel> channels = getNotificationManager().getNotificationChannels();
280+
if (channels.size() == 0)
281+
return null;
282+
NotificationChannelWrapper[] channelList = new NotificationChannelWrapper[channels.size()];
283+
int i = 0;
284+
for (NotificationChannel ch : channels) {
285+
channelList[i++] = notificationChannelToWrapper(ch);
286+
}
255287

256-
for (String k : channelIdsSet) {
257-
channels.add(getNotificationChannel(k));
288+
return channelList;
289+
} else {
290+
SharedPreferences prefs = mContext.getSharedPreferences(NOTIFICATION_CHANNELS_SHARED_PREFS, Context.MODE_PRIVATE);
291+
Set<String> channelIdsSet = prefs.getStringSet(NOTIFICATION_CHANNELS_SHARED_PREFS_KEY, new HashSet());
292+
if (channelIdsSet.size() == 0)
293+
return null;
294+
NotificationChannelWrapper[] channels = new NotificationChannelWrapper[channelIdsSet.size()];
295+
int i = 0;
296+
for (String k : channelIdsSet) {
297+
channels[i++] = getNotificationChannel(k);
298+
}
299+
return channels;
258300
}
259-
return channels.toArray();
260301
}
261302

262303
// This is called from Unity managed code to call AlarmManager to set a broadcast intent for sending a notification.
@@ -879,3 +920,23 @@ protected static synchronized Notification removeScheduledNotification(Integer i
879920
return mScheduledNotifications.remove(id);
880921
}
881922
}
923+
924+
// Provide a wrapper for NotificationChannel.
925+
// Create this wrapper for all Android versions as NotificationChannel is only available for Android O or above.
926+
class NotificationChannelWrapper {
927+
public String id;
928+
public String name;
929+
public int importance;
930+
public String description;
931+
public boolean enableLights;
932+
public boolean enableVibration;
933+
public boolean canBypassDnd;
934+
public boolean canShowBadge;
935+
public long[] vibrationPattern;
936+
public int lockscreenVisibility;
937+
}
938+
939+
// Implemented in C# to receive callback on notification show
940+
interface NotificationCallback {
941+
void onSentNotification(Notification notification);
942+
}

0 commit comments

Comments
 (0)