Skip to content

Commit 2c6a2b0

Browse files
committed
Changed channel payload format for backwards compatibility
* Change channel payload to use existing fields for better backwards compatibility. * Added init check to required Android Support compat revision 26.0.0 if running on and targeting O.
1 parent 6644b07 commit 2c6a2b0

File tree

6 files changed

+120
-65
lines changed

6 files changed

+120
-65
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/GenerateNotification.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import android.app.AlertDialog;
4646
import android.app.Notification;
4747
import android.app.PendingIntent;
48-
import android.content.ContentResolver;
4948
import android.content.ContentValues;
5049
import android.content.Context;
5150
import android.content.DialogInterface;
@@ -212,7 +211,7 @@ private static OneSignalNotificationBuilder getBaseOneSignalNotificationBuilder(
212211

213212
NotificationCompat.Builder notifBuilder;
214213
try {
215-
String channelId = NotificationChannelManager.createNotificationChannel(currentContext, notifJob.jsonPayload);
214+
String channelId = NotificationChannelManager.createNotificationChannel(currentContext, gcmBundle);
216215
// Will throw if app is using 26.0.0-beta1 or older of the support library.
217216
notifBuilder = new NotificationCompat.Builder(currentContext, channelId);
218217
} catch(Throwable t) {
@@ -242,8 +241,20 @@ private static OneSignalNotificationBuilder getBaseOneSignalNotificationBuilder(
242241
} catch (Throwable t) {} // Can throw if an old android support lib is used.
243242

244243
int notificationDefaults = 0;
245-
if (OneSignal.getVibrate(currentContext))
246-
notificationDefaults = Notification.DEFAULT_VIBRATE;
244+
245+
if (OneSignal.getVibrate(currentContext)) {
246+
if (gcmBundle.optBoolean("vib", true))
247+
notificationDefaults = Notification.DEFAULT_VIBRATE;
248+
if (gcmBundle.has("vib_pt")) {
249+
JSONArray json_vib_array = gcmBundle.optJSONArray("vib_pt");
250+
long[] long_array = new long[json_vib_array.length()];
251+
for (int i = 0; i < json_vib_array.length(); i++)
252+
long_array[i] = json_vib_array.optLong(i);
253+
254+
notificationDefaults = Notification.DEFAULT_VIBRATE;
255+
notifBuilder.setVibrate(long_array);
256+
}
257+
}
247258

248259
if (gcmBundle.has("ledc")) {
249260
try {
@@ -252,11 +263,12 @@ private static OneSignalNotificationBuilder getBaseOneSignalNotificationBuilder(
252263
} catch (Throwable t) {
253264
notificationDefaults |= Notification.DEFAULT_LIGHTS;
254265
} // Can throw if an old android support lib is used or parse error.
255-
} else
266+
}
267+
else
256268
notificationDefaults |= Notification.DEFAULT_LIGHTS;
257269

258270
try {
259-
int visibility = Notification.VISIBILITY_PUBLIC;
271+
int visibility = NotificationCompat.VISIBILITY_PUBLIC;
260272
if (gcmBundle.has("vis"))
261273
visibility = Integer.parseInt(gcmBundle.optString("vis"));
262274
notifBuilder.setVisibility(visibility);
@@ -281,9 +293,9 @@ private static OneSignalNotificationBuilder getBaseOneSignalNotificationBuilder(
281293
}
282294

283295
notifBuilder.setDefaults(notificationDefaults);
284-
285-
if (gcmBundle.optInt("pri", 0) > 9)
286-
notifBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
296+
297+
int payload_priority = gcmBundle.optInt("pri", 6);
298+
notifBuilder.setPriority(osPriorityToAndroidPriority(payload_priority));
287299

288300
oneSignalNotificationBuilder.compatBuilder = notifBuilder;
289301
return oneSignalNotificationBuilder;
@@ -939,4 +951,17 @@ private static void addAlertButtons(Context context, JSONObject gcmBundle, List<
939951
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Failed to parse buttons for alert dialog.", t);
940952
}
941953
}
954+
955+
private static int osPriorityToAndroidPriority(int priority) {
956+
if (priority > 9)
957+
return NotificationCompat.PRIORITY_MAX;
958+
if (priority > 7)
959+
return NotificationCompat.PRIORITY_HIGH;
960+
if (priority > 5)
961+
return NotificationCompat.PRIORITY_DEFAULT;
962+
if (priority > 3)
963+
return NotificationCompat.PRIORITY_LOW;
964+
965+
return NotificationCompat.PRIORITY_MIN;
966+
}
942967
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationChannelManager.java

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import android.net.Uri;
3636
import android.os.Build;
3737
import android.support.annotation.RequiresApi;
38+
import android.support.v4.app.NotificationManagerCompat;
3839

3940
import org.json.JSONArray;
4041
import org.json.JSONException;
@@ -80,62 +81,67 @@ static String createNotificationChannel(Context context, JSONObject jsonPayload)
8081
return createDefaultChannel(notificationManager);
8182

8283
try {
83-
return createChannel(context, notificationManager, jsonPayload.optJSONObject("chnl"));
84+
return createChannel(context, notificationManager, jsonPayload);
8485
} catch (JSONException e) {
8586
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Could not create notification channel due to JSON payload error!", e);
8687
}
8788

8889
return DEFAULT_CHANNEL_ID;
8990
}
9091

92+
// Language dependent fields will be passed localized
9193
@RequiresApi(api = Build.VERSION_CODES.O)
92-
private static String createChannel(Context context, NotificationManager notificationManager, JSONObject channelPayload) throws JSONException {
94+
private static String createChannel(Context context, NotificationManager notificationManager, JSONObject payload) throws JSONException {
95+
JSONObject channelPayload = payload.optJSONObject("chnl");
96+
9397
String channel_id = channelPayload.optString("id", DEFAULT_CHANNEL_ID);
9498
// Ensure we don't try to use the system reserved id
9599
if (channel_id.equals(NotificationChannel.DEFAULT_CHANNEL_ID))
96100
channel_id = DEFAULT_CHANNEL_ID;
97-
98-
int importance = channelPayload.optInt("imp", NotificationManager.IMPORTANCE_DEFAULT);
101+
99102
String channel_name = channelPayload.optString("nm", "Miscellaneous");
100-
103+
104+
int importance = priorityToImportance(payload.optInt("pri", 6));
101105
NotificationChannel channel = new NotificationChannel(channel_id, channel_name, importance);
102106

103-
if (channelPayload.has("grp")) {
104-
String group_id = channelPayload.optString("grp");
107+
if (channelPayload.has("grp_id")) {
108+
String group_id = channelPayload.optString("grp_id");
105109
CharSequence group_name = channelPayload.optString("grp_nm");
106110
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(group_id, group_name));
107111
channel.setGroup(group_id);
108112
}
109113

110-
channel.enableLights(channelPayload.optBoolean("lght", true));
111-
if (channelPayload.has("ledc")) {
112-
BigInteger ledColor = new BigInteger(channelPayload.optString("ledc"), 16);
114+
channel.enableLights(payload.optBoolean("led", true));
115+
if (payload.has("ledc")) {
116+
BigInteger ledColor = new BigInteger(payload.optString("ledc"), 16);
113117
channel.setLightColor(ledColor.intValue());
114118
}
115119

116-
channel.enableVibration(channelPayload.optBoolean("vib", true));
117-
if (channelPayload.has("vib_pt")) {
118-
JSONArray json_vib_array = channelPayload.optJSONArray("vib_pt");
120+
channel.enableVibration(payload.optBoolean("vib", true));
121+
if (payload.has("vib_pt")) {
122+
JSONArray json_vib_array = payload.optJSONArray("vib_pt");
119123
long[] long_array = new long[json_vib_array.length()];
120124
for (int i = 0; i < json_vib_array.length(); i++)
121125
long_array[i] = json_vib_array.optLong(i);
122126
channel.setVibrationPattern(long_array);
123127
}
124128

125-
if (channelPayload.has("snd_nm")) {
129+
if (payload.has("sound")) {
126130
// Sound will only play if Importance is set to High or Urgent
127-
Uri uri = OSUtils.getSoundUri(context, channelPayload.optString("snd_nm", null));
128-
if (uri!= null)
131+
String sound = payload.optString("sound", null);
132+
Uri uri = OSUtils.getSoundUri(context, sound);
133+
if (uri != null)
129134
channel.setSound(uri, null);
135+
else if ("null".equals(sound) || "nil".equals(sound))
136+
channel.setSound(null, null);
137+
// null = None for a sound.
130138
}
131-
else if (!channelPayload.optBoolean("snd", true))
132-
channel.setSound(null, null);
133139
// Setting sound to null makes it 'None' in the Settings.
134140
// Otherwise not calling setSound makes it the default notification sound.
135141

136-
channel.setLockscreenVisibility(channelPayload.optInt("lck", Notification.VISIBILITY_PUBLIC));
137-
channel.setShowBadge(channelPayload.optBoolean("bdg", true));
138-
channel.setBypassDnd(channelPayload.optBoolean("bdnd", false));
142+
channel.setLockscreenVisibility(payload.optInt("vis", Notification.VISIBILITY_PUBLIC));
143+
channel.setShowBadge(payload.optBoolean("bdg", true));
144+
channel.setBypassDnd(payload.optBoolean("bdnd", false));
139145

140146
notificationManager.createNotificationChannel(channel);
141147
return channel_id;
@@ -184,4 +190,19 @@ static void processChannelList(Context context, JSONObject payload) {
184190
notificationManager.deleteNotificationChannel(id);
185191
}
186192
}
193+
194+
private static int priorityToImportance(int priority) {
195+
if (priority > 9)
196+
return NotificationManagerCompat.IMPORTANCE_MAX;
197+
if (priority > 7)
198+
return NotificationManagerCompat.IMPORTANCE_HIGH;
199+
if (priority > 5)
200+
return NotificationManagerCompat.IMPORTANCE_DEFAULT;
201+
if (priority > 3)
202+
return NotificationManagerCompat.IMPORTANCE_LOW;
203+
if (priority > 1)
204+
return NotificationManagerCompat.IMPORTANCE_MIN;
205+
206+
return NotificationManagerCompat.IMPORTANCE_NONE;
207+
}
187208
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSUtils.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Modified MIT License
33
*
4-
* Copyright 2016 OneSignal
4+
* Copyright 2017 OneSignal
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -35,6 +35,7 @@
3535
import android.net.ConnectivityManager;
3636
import android.net.NetworkInfo;
3737
import android.net.Uri;
38+
import android.os.Build;
3839
import android.os.Bundle;
3940
import android.os.Handler;
4041
import android.os.Looper;
@@ -49,7 +50,7 @@ class OSUtils {
4950

5051
static final int UNINITIALIZABLE_STATUS = -999;
5152

52-
int initializationChecker(int deviceType, String oneSignalAppId) {
53+
int initializationChecker(Context context, int deviceType, String oneSignalAppId) {
5354
int subscribableStatus = 1;
5455

5556
try {
@@ -85,12 +86,20 @@ int initializationChecker(int deviceType, String oneSignalAppId) {
8586
try {
8687
Class.forName("android.support.v4.content.WakefulBroadcastReceiver");
8788
Class.forName("android.support.v4.app.NotificationManagerCompat");
89+
90+
// If running on Android O and targeting O we need version 26.0.0 for
91+
// the new compat NotificationCompat.Builder constructor.
92+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
93+
&& getTargetSdkVersion(context) >= Build.VERSION_CODES.O) {
94+
// Class was added in 26.0.0-beta2
95+
Class.forName("android.support.v4.app.JobIntentService");
96+
}
8897
} catch (ClassNotFoundException e) {
89-
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "The included Android Support Library v4 is to old or incomplete. Please update your project's android-support-v4.jar to the latest revision.", e);
98+
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "The included Android Support Library is to old or incomplete. Please update to the 26.0.0 revision or newer.", e);
9099
subscribableStatus = -5;
91100
}
92101
} catch (ClassNotFoundException e) {
93-
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "Could not find the Android Support Library v4. Please make sure android-support-v4.jar has been correctly added to your project.", e);
102+
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "Could not find the Android Support Library. Please make sure it has been correctly added to your project.", e);
94103
subscribableStatus = -3;
95104
}
96105

@@ -195,6 +204,18 @@ static void runOnMainUIThread(Runnable runnable) {
195204
}
196205
}
197206

207+
static int getTargetSdkVersion(Context context) {
208+
String packageName = context.getPackageName();
209+
PackageManager packageManager = context.getPackageManager();
210+
try {
211+
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
212+
return applicationInfo.targetSdkVersion;
213+
} catch (PackageManager.NameNotFoundException e) {
214+
e.printStackTrace();
215+
}
216+
217+
return Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1;
218+
}
198219

199220
static boolean isValidResourceName(String name) {
200221
return (name != null && !name.matches("^[0-9]"));

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,6 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
29-
30-
// TODO: Android O branch todo
31-
// 1. Add Android O channel methods
32-
// a. Set default channel id.
33-
// b. Set all available options with a single object.
34-
// - Create in a generic way that can be used by wrapper SDK such as Unity
35-
// 2. GCM broadcast receiver can no longer start the GcmIntentService when app is swiped away.
36-
// - Service may need to be started differently or all work must be done from the receiver directly then.
37-
// 3. filterOtherGCMReceivers no long works.
38-
// - Google Play services is no longer starting the broadcast receiver as an order broadcast
39-
// 4. Fix bug where restored notifications are vibrating and playing the sound again.
40-
// - This is due to the channel now being in control of these settings.
41-
// - Fixed cold restarts of the app. App updates and reboots still notify again.
42-
43-
4428
package com.onesignal;
4529

4630
import java.io.PrintWriter;
@@ -363,7 +347,7 @@ public static void init(Context context, String googleProjectNumber, String oneS
363347

364348
osUtils = new OSUtils();
365349
deviceType = osUtils.getDeviceType();
366-
subscribableStatus = osUtils.initializationChecker(deviceType, oneSignalAppId);
350+
subscribableStatus = osUtils.initializationChecker(context, deviceType, oneSignalAppId);
367351
if (subscribableStatus == OSUtils.UNINITIALIZABLE_STATUS)
368352
return;
369353

OneSignalSDK/unittest/src/test/java/com/test/onesignal/GenerateNotificationRunner.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,6 @@ private static Bundle getBundleWithAllOptionsSet() {
902902

903903
bundle.putString("title", "Test H");
904904
bundle.putString("alert", "Test B");
905-
bundle.putString("bgn", "1");
906905
bundle.putString("vis", "0");
907906
bundle.putString("bgac", "FF0000FF");
908907
bundle.putString("from", "703322744261");

OneSignalSDK/unittest/src/test/java/com/test/onesignal/NotificationChannelManagerRunner.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,18 @@ public void createNotificationChannelWithALlOptionsl() throws Exception {
9999

100100
chnl.put("id", "test_id");
101101
chnl.put("nm", "Test Name");
102-
chnl.put("grp", "grp_id");
102+
chnl.put("grp_id", "grp_id");
103103
chnl.put("grp_nm", "Group Name");
104-
chnl.put("imp", NotificationManager.IMPORTANCE_MAX);
105-
chnl.put("lght", false);
106-
chnl.put("ledc", "FFFF0000");
107-
chnl.put("vib", false);
108-
chnl.put("vib_pt", new JSONArray("[1,2,3,4]"));
109-
chnl.put("snd_nm", "notification");
110-
chnl.put("lck", Notification.VISIBILITY_SECRET);
111-
chnl.put("bdg", true);
112-
chnl.put("bdnd", true);
104+
105+
payload.put("pri", 10);
106+
payload.put("led", false);
107+
payload.put("ledc", "FFFF0000");
108+
payload.put("vib", false);
109+
payload.put("vib_pt", new JSONArray("[1,2,3,4]"));
110+
payload.put("sound", "notification");
111+
payload.put("vis", Notification.VISIBILITY_SECRET);
112+
payload.put("bdg", true);
113+
payload.put("bdnd", true);
113114

114115
payload.put("chnl", chnl);
115116

@@ -172,8 +173,10 @@ public void processPayloadCreatingNewChannel() throws Exception {
172173

173174
JSONArray channelList = new JSONArray();
174175
JSONObject channelItem = new JSONObject();
175-
176-
channelItem.put("id", "OS_id1");
176+
JSONObject channelItemChnl = new JSONObject();
177+
178+
channelItemChnl.put("id", "OS_id1");
179+
channelItem.put("chnl", channelItemChnl);
177180

178181
channelList.put(channelItem);
179182
JSONObject payload = new JSONObject();
@@ -197,8 +200,10 @@ JSONObject createBasicChannelListPayload() throws JSONException {
197200

198201
JSONArray channelList = new JSONArray();
199202
JSONObject channelItem = new JSONObject();
203+
JSONObject channelItemChnl = new JSONObject();
200204

201-
channelItem.put("id", "OS_id1");
205+
channelItemChnl.put("id", "OS_id1");
206+
channelItem.put("chnl", channelItemChnl);
202207

203208
channelList.put(channelItem);
204209
JSONObject payload = new JSONObject();

0 commit comments

Comments
 (0)