1
1
package com .unity .androidnotifications ;
2
2
3
+ import android .annotation .TargetApi ;
3
4
import android .app .Activity ;
4
5
import android .app .ActivityManager ;
5
6
import android .app .AlarmManager ;
6
7
import android .app .Notification ;
8
+ import android .app .NotificationChannel ;
7
9
import android .app .NotificationManager ;
8
10
import android .app .PendingIntent ;
9
11
import android .content .BroadcastReceiver ;
@@ -112,14 +114,8 @@ public static UnityNotificationManager getNotificationManagerImpl(Context contex
112
114
113
115
// Called from managed code.
114
116
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 )
121
118
mUnityNotificationManager = new UnityNotificationManager (context , activity );
122
- }
123
119
124
120
return mUnityNotificationManager ;
125
121
}
@@ -138,8 +134,6 @@ public void setNotificationCallback(NotificationCallback notificationCallback) {
138
134
UnityNotificationManager .mNotificationCallback = notificationCallback ;
139
135
}
140
136
141
- // Register a new notification channel.
142
- // This function will only be called for devices which are low than Android O.
143
137
public void registerNotificationChannel (
144
138
String id ,
145
139
String name ,
@@ -151,42 +145,56 @@ public void registerNotificationChannel(
151
145
boolean canShowBadge ,
152
146
long [] vibrationPattern ,
153
147
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?
157
163
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 ();
162
168
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 ();
166
172
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 );
176
182
177
- editor .apply ();
183
+ editor .apply ();
184
+ }
178
185
}
179
186
180
187
protected static String getSharedPrefsNameByChannelId (String id )
181
188
{
182
189
return String .format ("unity_notification_channel_%s" , id );
183
190
}
184
191
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 ) {
188
193
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 );
190
198
}
191
199
192
200
SharedPreferences prefs = context .getSharedPreferences (getSharedPrefsNameByChannelId (id ), Context .MODE_PRIVATE );
@@ -219,44 +227,77 @@ protected static NotificationChannelWrapper getNotificationChannel(Context conte
219
227
return channel ;
220
228
}
221
229
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 ) {
225
251
return UnityNotificationManager .getNotificationChannel (mContext , id );
226
252
}
227
253
228
- // Delete a notification channel by id.
229
- // This function will only be called for devices which are low than Android O.
230
254
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 ());
233
260
234
- if (!channelIds .contains (id ))
235
- return ;
261
+ if (!channelIds .contains (id ))
262
+ return ;
236
263
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 ();
242
270
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
+ }
246
275
}
247
276
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
+ }
255
287
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 ;
258
300
}
259
- return channels .toArray ();
260
301
}
261
302
262
303
// 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
879
920
return mScheduledNotifications .remove (id );
880
921
}
881
922
}
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