41
41
import org .json .JSONObject ;
42
42
43
43
import java .math .BigInteger ;
44
+ import java .util .HashSet ;
45
+ import java .util .List ;
46
+ import java .util .Set ;
44
47
45
48
class NotificationChannelManager {
46
49
@@ -51,89 +54,90 @@ class NotificationChannelManager {
51
54
private static final String DEFAULT_CHANNEL_ID = "fcm_fallback_notification_channel" ;
52
55
53
56
static String createNotificationChannel (Context context , JSONObject jsonPayload ) {
54
- OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "Build.VERSION.SDK_INT: " + Build .VERSION .SDK_INT );
55
57
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O )
56
58
return DEFAULT_CHANNEL_ID ;
57
59
58
- JSONObject payload = jsonPayload ;
59
-
60
60
// To test with additional data
61
- // JSONObject customJson = null;
62
- // try {
63
- // customJson = new JSONObject(jsonPayload.optString("custom"));
64
- // } catch (JSONException e) {
65
- // e.printStackTrace();
66
- // }
67
- // JSONObject payload = customJson.optJSONObject("a");
61
+ JSONObject customJson = null ;
62
+ try {
63
+ customJson = new JSONObject (jsonPayload .optString ("custom" ));
64
+ } catch (JSONException e ) {
65
+ e .printStackTrace ();
66
+ }
67
+ jsonPayload = customJson .optJSONObject ("a" );
68
68
69
69
NotificationManager notificationManager =
70
- (NotificationManager )context .getSystemService (Context .NOTIFICATION_SERVICE );
70
+ (NotificationManager )context .getSystemService (Context .NOTIFICATION_SERVICE );
71
71
72
72
// TODO: Check for oth_chnl key, if this existing and chanel is already registered use this id
73
73
// to allow any channels created outside of the SDK.
74
74
75
- if (!payload .has ("chnl" ))
75
+ if (!jsonPayload .has ("chnl" ))
76
76
return createDefaultChannel (notificationManager );
77
-
77
+
78
78
try {
79
- JSONObject channelPayload = payload .getJSONObject ("chnl" );
80
-
81
- String channel_id = channelPayload .optString ("id" , DEFAULT_CHANNEL_ID );
82
- // Ensure we don't try to use the system reserved id
83
- if (channel_id .equals (NotificationChannel .DEFAULT_CHANNEL_ID ))
84
- channel_id = DEFAULT_CHANNEL_ID ;
85
-
86
- int importance = channelPayload .optInt ("imp" , NotificationManager .IMPORTANCE_DEFAULT );
87
- String channel_name = channelPayload .optString ("nm" , "Miscellaneous" );
88
-
89
- NotificationChannel channel = new NotificationChannel (channel_id , channel_name , importance );
90
-
91
- if (channelPayload .has ("grp" )) {
92
- String group_id = channelPayload .optString ("grp" );
93
- CharSequence group_name = channelPayload .optString ("grp_nm" );
94
- notificationManager .createNotificationChannelGroup (new NotificationChannelGroup (group_id , group_name ));
95
- channel .setGroup (group_id );
96
- }
97
-
98
- channel .enableLights (channelPayload .optBoolean ("lght" , true ));
99
- if (channelPayload .has ("ledc" )) {
100
- BigInteger ledColor = new BigInteger (channelPayload .optString ("ledc" ), 16 );
101
- channel .setLightColor (ledColor .intValue ());
102
- }
103
-
104
- channel .enableVibration (channelPayload .optBoolean ("vib" , true ));
105
- if (channelPayload .has ("vib_pt" )) {
106
- JSONArray json_vib_array = channelPayload .optJSONArray ("vib_pt" );
107
- long [] long_array = new long [json_vib_array .length ()];
108
- for (int i = 0 ; i < json_vib_array .length (); i ++)
109
- long_array [i ] = json_vib_array .optLong (i );
110
- channel .setVibrationPattern (long_array );
111
- }
112
-
113
- if (channelPayload .has ("snd_nm" )) {
114
- // Sound will only play if Importance is set to High or Urgent
115
- Uri uri = OSUtils .getSoundUri (context , channelPayload .optString ("snd_nm" , null ));
116
- if (uri != null )
117
- channel .setSound (uri , null );
118
- }
119
- else if (!channelPayload .optBoolean ("snd" , true ))
120
- channel .setSound (null , null );
121
- // Setting sound to null makes it 'None' in the Settings.
122
- // Otherwise not calling setSound makes it the default notification sound.
123
-
124
- channel .setLockscreenVisibility (channelPayload .optInt ("lck" , Notification .VISIBILITY_PUBLIC ));
125
- channel .enableVibration (channelPayload .optBoolean ("lght" , true ));
126
- channel .setShowBadge (channelPayload .optBoolean ("bdg" , true ));
127
- channel .setBypassDnd (channelPayload .optBoolean ("bdnd" , false ));
128
-
129
- notificationManager .createNotificationChannel (channel );
130
- return channel_id ;
79
+ return createChannel (context , notificationManager , jsonPayload );
131
80
} catch (JSONException e ) {
132
- OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "Count not create notification channel due to JSON payload error!" , e );
81
+ OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "Could not create notification channel due to JSON payload error!" , e );
133
82
}
134
83
135
84
return DEFAULT_CHANNEL_ID ;
136
85
}
86
+
87
+ @ RequiresApi (api = Build .VERSION_CODES .O )
88
+ private static String createChannel (Context context , NotificationManager notificationManager , JSONObject payload ) throws JSONException {
89
+ JSONObject channelPayload = payload .getJSONObject ("chnl" );
90
+
91
+ String channel_id = channelPayload .optString ("id" , DEFAULT_CHANNEL_ID );
92
+ // Ensure we don't try to use the system reserved id
93
+ if (channel_id .equals (NotificationChannel .DEFAULT_CHANNEL_ID ))
94
+ channel_id = DEFAULT_CHANNEL_ID ;
95
+
96
+ int importance = channelPayload .optInt ("imp" , NotificationManager .IMPORTANCE_DEFAULT );
97
+ String channel_name = channelPayload .optString ("nm" , "Miscellaneous" );
98
+
99
+ NotificationChannel channel = new NotificationChannel (channel_id , channel_name , importance );
100
+
101
+ if (channelPayload .has ("grp" )) {
102
+ String group_id = channelPayload .optString ("grp" );
103
+ CharSequence group_name = channelPayload .optString ("grp_nm" );
104
+ notificationManager .createNotificationChannelGroup (new NotificationChannelGroup (group_id , group_name ));
105
+ channel .setGroup (group_id );
106
+ }
107
+
108
+ channel .enableLights (channelPayload .optBoolean ("lght" , true ));
109
+ if (channelPayload .has ("ledc" )) {
110
+ BigInteger ledColor = new BigInteger (channelPayload .optString ("ledc" ), 16 );
111
+ channel .setLightColor (ledColor .intValue ());
112
+ }
113
+
114
+ channel .enableVibration (channelPayload .optBoolean ("vib" , true ));
115
+ if (channelPayload .has ("vib_pt" )) {
116
+ JSONArray json_vib_array = channelPayload .optJSONArray ("vib_pt" );
117
+ long [] long_array = new long [json_vib_array .length ()];
118
+ for (int i = 0 ; i < json_vib_array .length (); i ++)
119
+ long_array [i ] = json_vib_array .optLong (i );
120
+ channel .setVibrationPattern (long_array );
121
+ }
122
+
123
+ if (channelPayload .has ("snd_nm" )) {
124
+ // Sound will only play if Importance is set to High or Urgent
125
+ Uri uri = OSUtils .getSoundUri (context , channelPayload .optString ("snd_nm" , null ));
126
+ if (uri != null )
127
+ channel .setSound (uri , null );
128
+ }
129
+ else if (!channelPayload .optBoolean ("snd" , true ))
130
+ channel .setSound (null , null );
131
+ // Setting sound to null makes it 'None' in the Settings.
132
+ // Otherwise not calling setSound makes it the default notification sound.
133
+
134
+ channel .setLockscreenVisibility (channelPayload .optInt ("lck" , Notification .VISIBILITY_PUBLIC ));
135
+ channel .setShowBadge (channelPayload .optBoolean ("bdg" , true ));
136
+ channel .setBypassDnd (channelPayload .optBoolean ("bdnd" , false ));
137
+
138
+ notificationManager .createNotificationChannel (channel );
139
+ return channel_id ;
140
+ }
137
141
138
142
@ RequiresApi (api = Build .VERSION_CODES .O )
139
143
private static String createDefaultChannel (NotificationManager notificationManager ) {
@@ -151,7 +155,32 @@ private static String createDefaultChannel(NotificationManager notificationManag
151
155
// TODO: 1. Check JSONObject for a chnl_lst key.
152
156
// 2. Create a set of new channels.
153
157
// 3. Remove any other 'OS_' not defined in this payload
154
- private static void processChannelList (JSONObject payload ) {
155
-
158
+ static void processChannelList (Context context , JSONObject payload ) {
159
+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O )
160
+ return ;
161
+
162
+ if (!payload .has ("chnl_lst" ))
163
+ return ;
164
+
165
+ NotificationManager notificationManager =
166
+ (NotificationManager )context .getSystemService (Context .NOTIFICATION_SERVICE );
167
+
168
+ Set <String > sycnedChannelSet = new HashSet <>();
169
+ JSONArray chnlList = payload .optJSONArray ("chnl_lst" );
170
+ int jsonArraySize = chnlList .length ();
171
+ for (int i = 0 ; i < jsonArraySize ; i ++) {
172
+ try {
173
+ sycnedChannelSet .add (createChannel (context , notificationManager , payload ));
174
+ } catch (JSONException e ) {
175
+ OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "Could not create notification channel due to JSON payload error!" , e );
176
+ }
177
+ }
178
+
179
+ List <NotificationChannel > existingChannels = notificationManager .getNotificationChannels ();
180
+ for (NotificationChannel existingChannel : existingChannels ) {
181
+ String id = existingChannel .getId ();
182
+ if (id .startsWith ("OS_" ) && !sycnedChannelSet .contains (id ))
183
+ notificationManager .deleteNotificationChannel (id );
184
+ }
156
185
}
157
186
}
0 commit comments