44
44
import android .app .Activity ;
45
45
import android .app .AlertDialog ;
46
46
import android .app .Notification ;
47
- import android .app .NotificationChannel ;
48
- import android .app .NotificationChannelGroup ;
49
- import android .app .NotificationManager ;
50
47
import android .app .PendingIntent ;
51
48
import android .content .ContentResolver ;
52
49
import android .content .ContentValues ;
61
58
import android .graphics .BitmapFactory ;
62
59
import android .net .Uri ;
63
60
import android .os .Build ;
64
- import android .support .annotation .RequiresApi ;
65
61
import android .support .v4 .app .NotificationCompat ;
66
62
import android .support .v4 .app .NotificationManagerCompat ;
67
63
import android .text .SpannableString ;
@@ -78,12 +74,6 @@ class GenerateNotification {
78
74
private static Resources contextResources = null ;
79
75
private static Class <?> notificationOpenedClass ;
80
76
private static boolean openerIsBroadcast ;
81
-
82
- // Can't create a channel with the id 'miscellaneous' as an exception is thrown.
83
- // Using it results in the notification not being displayed.
84
- // private static final String DEFAULT_CHANNEL_ID = "miscellaneous"; // NotificationChannel.DEFAULT_CHANNEL_ID;
85
-
86
- private static final String DEFAULT_CHANNEL_ID = "fcm_fallback_notification_channel" ;
87
77
88
78
private static class OneSignalNotificationBuilder {
89
79
NotificationCompat .Builder compatBuilder ;
@@ -182,105 +172,6 @@ else if (i == 2)
182
172
});
183
173
}
184
174
185
- // TODO: Need to handle delete, do so on cold start up.
186
- private static String createNotificationChannel (NotificationGenerationJob notifJob ) {
187
- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O )
188
- return DEFAULT_CHANNEL_ID ;
189
-
190
- // TODO: Can new api be used if not targeting O but device is and have a new enough library?
191
- // If not then check for this and return.
192
-
193
- JSONObject payload = notifJob .jsonPayload ;
194
-
195
- // Testing with additional data
196
- // JSONObject customJson = null;
197
- // try {
198
- // customJson = new JSONObject(notifJob.jsonPayload.optString("custom"));
199
- // } catch (JSONException e) {
200
- // e.printStackTrace();
201
- // }
202
- // JSONObject payload = customJson.optJSONObject("a");
203
-
204
- if (!payload .has ("chnl" ))
205
- return createDefaultChannel ();
206
-
207
- try {
208
- JSONObject channelPayload = payload .getJSONObject ("chnl" );
209
-
210
- NotificationManager notificationManager =
211
- (NotificationManager ) currentContext .getSystemService (Context .NOTIFICATION_SERVICE );
212
-
213
- String channel_id = channelPayload .optString ("id" , DEFAULT_CHANNEL_ID );
214
- // Ensure we don't try to use the system reserved id
215
- if (channel_id .equals (NotificationChannel .DEFAULT_CHANNEL_ID ))
216
- channel_id = DEFAULT_CHANNEL_ID ;
217
-
218
- int importance = channelPayload .optInt ("imp" , NotificationManager .IMPORTANCE_DEFAULT );
219
- String channel_name = channelPayload .optString ("nm" , "Miscellaneous" );
220
-
221
- NotificationChannel channel = new NotificationChannel (channel_id , channel_name , importance );
222
-
223
- if (channelPayload .has ("grp" )) {
224
- String group_id = channelPayload .optString ("grp" );
225
- CharSequence group_name = channelPayload .optString ("grp_nm" );
226
- notificationManager .createNotificationChannelGroup (new NotificationChannelGroup (group_id , group_name ));
227
- channel .setGroup (group_id );
228
- }
229
-
230
- channel .enableLights (channelPayload .optBoolean ("lght" , true ));
231
- if (channelPayload .has ("ledc" )) {
232
- BigInteger ledColor = new BigInteger (channelPayload .optString ("ledc" ), 16 );
233
- channel .setLightColor (ledColor .intValue ());
234
- }
235
-
236
- channel .enableVibration (channelPayload .optBoolean ("vib" , true ));
237
- if (channelPayload .has ("vib_pt" )) {
238
- JSONArray json_vib_array = channelPayload .optJSONArray ("vib_pt" );
239
- long [] long_array = new long [json_vib_array .length ()];
240
- for (int i = 0 ; i < json_vib_array .length (); i ++)
241
- long_array [i ] = json_vib_array .optLong (i );
242
- channel .setVibrationPattern (long_array );
243
- }
244
-
245
- if (channelPayload .has ("snd_nm" )) {
246
- // Sound will only play if Importance is set to High or Urgent
247
- Uri uri = getCustomSound (channelPayload .optString ("snd_nm" , null ));
248
- OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "##################### channel.seSound():" + uri );
249
- channel .setSound (uri , null );
250
- }
251
- else if (!channelPayload .optBoolean ("snd" , true ))
252
- channel .setSound (null , null );
253
- // Setting sound to null makes it 'None' in the Settings.
254
- // Otherwise not calling setSound makes it the default notification sound.
255
-
256
- channel .setLockscreenVisibility (channelPayload .optInt ("lck" , Notification .VISIBILITY_PUBLIC ));
257
- channel .enableVibration (channelPayload .optBoolean ("lght" , true ));
258
- channel .setShowBadge (channelPayload .optBoolean ("bdg" , true ));
259
- channel .setBypassDnd (channelPayload .optBoolean ("bdnd" , false ));
260
-
261
- notificationManager .createNotificationChannel (channel );
262
- return channel_id ;
263
- } catch (JSONException e ) {
264
- OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "Count not create notification channel due to JSON payload error!" , e );
265
- }
266
-
267
- return DEFAULT_CHANNEL_ID ;
268
- }
269
-
270
- @ RequiresApi (api = Build .VERSION_CODES .O )
271
- private static String createDefaultChannel () {
272
- NotificationManager notificationManager =
273
- (NotificationManager ) currentContext .getSystemService (Context .NOTIFICATION_SERVICE );
274
-
275
- NotificationChannel channel = new NotificationChannel (DEFAULT_CHANNEL_ID ,
276
- "Miscellaneous" ,
277
- NotificationManager .IMPORTANCE_DEFAULT );
278
- channel .enableLights (true );
279
- channel .enableVibration (true );
280
- notificationManager .createNotificationChannel (channel );
281
- return DEFAULT_CHANNEL_ID ;
282
- }
283
-
284
175
private static CharSequence getTitle (JSONObject gcmBundle ) {
285
176
CharSequence title = gcmBundle .optString ("title" , null );
286
177
@@ -321,7 +212,7 @@ private static OneSignalNotificationBuilder getBaseOneSignalNotificationBuilder(
321
212
322
213
NotificationCompat .Builder notifBuilder ;
323
214
try {
324
- String channelId = createNotificationChannel (notifJob );
215
+ String channelId = NotificationChannelManager . createNotificationChannel (currentContext , notifJob . jsonPayload );
325
216
// Will throw if app is using 26.0.0-beta1 or older of the support library.
326
217
notifBuilder = new NotificationCompat .Builder (currentContext , channelId );
327
218
} catch (Throwable t ) {
@@ -382,7 +273,7 @@ private static OneSignalNotificationBuilder getBaseOneSignalNotificationBuilder(
382
273
notifBuilder .setStyle (new NotificationCompat .BigPictureStyle ().bigPicture (bigPictureIcon ).setSummaryText (message ));
383
274
384
275
if (isSoundEnabled (gcmBundle )) {
385
- Uri soundUri = getCustomSound ( gcmBundle .optString ("sound" , null ));
276
+ Uri soundUri = OSUtils . getSoundUri ( currentContext , gcmBundle .optString ("sound" , null ));
386
277
if (soundUri != null )
387
278
notifBuilder .setSound (soundUri );
388
279
else
@@ -819,10 +710,6 @@ private static Integer safeGetColorFromHex(JSONObject gcmBundle, String colorKey
819
710
return null ;
820
711
}
821
712
822
- private static boolean isValidResourceName (String name ) {
823
- return (name != null && !name .matches ("^[0-9]" ));
824
- }
825
-
826
713
private static Bitmap getLargeIcon (JSONObject gcmBundle ) {
827
714
if (android .os .Build .VERSION .SDK_INT < android .os .Build .VERSION_CODES .HONEYCOMB )
828
715
return null ;
@@ -927,7 +814,7 @@ private static int getResourceIcon(String iconName) {
927
814
return 0 ;
928
815
929
816
String trimmedIconName = iconName .trim ();
930
- if (!isValidResourceName (trimmedIconName ))
817
+ if (!OSUtils . isValidResourceName (trimmedIconName ))
931
818
return 0 ;
932
819
933
820
int notificationIcon = getDrawableId (trimmedIconName );
@@ -977,22 +864,6 @@ private static boolean isSoundEnabled(JSONObject gcmBundle) {
977
864
return OneSignal .getSoundEnabled (currentContext );
978
865
}
979
866
980
- private static Uri getCustomSound (String sound ) {
981
- int soundId ;
982
-
983
- if (isValidResourceName (sound )) {
984
- soundId = contextResources .getIdentifier (sound , "raw" , packageName );
985
- if (soundId != 0 )
986
- return Uri .parse (ContentResolver .SCHEME_ANDROID_RESOURCE + "://" + packageName + "/" + soundId );
987
- }
988
-
989
- soundId = contextResources .getIdentifier ("onesignal_default_sound" , "raw" , packageName );
990
- if (soundId != 0 )
991
- return Uri .parse (ContentResolver .SCHEME_ANDROID_RESOURCE + "://" + packageName + "/" + soundId );
992
-
993
- return null ;
994
- }
995
-
996
867
// Android 5.0 accent color to use, only works when AndroidManifest.xml is targetSdkVersion >= 21
997
868
private static BigInteger getAccentColor (JSONObject gcmBundle ) {
998
869
try {
0 commit comments