Skip to content

Commit 8868ed6

Browse files
committed
Added a restore channel
* Fixed some channel defaults
1 parent 2a5f4d6 commit 8868ed6

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

OneSignalSDK/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77
maven { url 'https://maven.google.com' }
88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.0.0-alpha9'
10+
classpath 'com.android.tools.build:gradle:3.0.0-beta1'
1111

1212
// NOTE: Do not place your application dependencies here; they belong
1313
// in the individual module build.gradle files

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private static OneSignalNotificationBuilder getBaseOneSignalNotificationBuilder(
211211

212212
NotificationCompat.Builder notifBuilder;
213213
try {
214-
String channelId = NotificationChannelManager.createNotificationChannel(currentContext, gcmBundle);
214+
String channelId = NotificationChannelManager.createNotificationChannel(notifJob);
215215
// Will throw if app is using 26.0.0-beta1 or older of the support library.
216216
notifBuilder = new NotificationCompat.Builder(currentContext, channelId);
217217
} catch(Throwable t) {

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,22 @@ class NotificationChannelManager {
5151
// Can't create a channel with the id 'miscellaneous' as an exception is thrown.
5252
// Using it results in the notification not being displayed.
5353
// private static final String DEFAULT_CHANNEL_ID = "miscellaneous"; // NotificationChannel.DEFAULT_CHANNEL_ID;
54-
54+
5555
private static final String DEFAULT_CHANNEL_ID = "fcm_fallback_notification_channel";
56+
private static final String RESTORE_CHANNEL_ID = "restored_OS_notifications";
5657

57-
static String createNotificationChannel(Context context, JSONObject jsonPayload) {
58+
static String createNotificationChannel(NotificationGenerationJob notifJob) {
5859
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
5960
return DEFAULT_CHANNEL_ID;
6061

61-
//// To test with additional data
62-
// JSONObject customJson = null;
63-
// try {
64-
// customJson = new JSONObject(jsonPayload.optString("custom"));
65-
// } catch (JSONException e) {
66-
// e.printStackTrace();
67-
// }
68-
// jsonPayload = customJson.optJSONObject("a");
69-
62+
Context context = notifJob.context;
63+
JSONObject jsonPayload = notifJob.jsonPayload;
64+
7065
NotificationManager notificationManager =
71-
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
66+
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
67+
68+
if (notifJob.restoring)
69+
return createRestoreChannel(notificationManager);
7270

7371
// Allow channels created outside the SDK
7472
if (jsonPayload.has("oth_chnl")) {
@@ -100,7 +98,7 @@ private static String createChannel(Context context, NotificationManager notific
10098
JSONObject channelPayload = null;
10199
if (objChannelPayload instanceof String)
102100
channelPayload = new JSONObject((String)objChannelPayload);
103-
else if (objChannelPayload instanceof JSONObject)
101+
else
104102
channelPayload = (JSONObject)objChannelPayload;
105103

106104
String channel_id = channelPayload.optString("id", DEFAULT_CHANNEL_ID);
@@ -128,21 +126,21 @@ else if (objChannelPayload instanceof JSONObject)
128126
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(group_id, group_name));
129127
channel.setGroup(group_id);
130128
}
131-
132-
channel.enableLights(payload.optInt("led", 1) == 1);
129+
133130
if (payload.has("ledc")) {
134131
BigInteger ledColor = new BigInteger(payload.optString("ledc"), 16);
135132
channel.setLightColor(ledColor.intValue());
136133
}
134+
channel.enableLights(payload.optInt("led", 1) == 1);
137135

138-
channel.enableVibration(payload.optInt("vib", 1) == 1);
139136
if (payload.has("vib_pt")) {
140137
JSONArray json_vib_array = payload.optJSONArray("vib_pt");
141138
long[] long_array = new long[json_vib_array.length()];
142139
for (int i = 0; i < json_vib_array.length(); i++)
143140
long_array[i] = json_vib_array.optLong(i);
144141
channel.setVibrationPattern(long_array);
145142
}
143+
channel.enableVibration(payload.optInt("vib", 1) == 1);
146144

147145
if (payload.has("sound")) {
148146
// Sound will only play if Importance is set to High or Urgent
@@ -157,7 +155,7 @@ else if ("null".equals(sound) || "nil".equals(sound))
157155
// Setting sound to null makes it 'None' in the Settings.
158156
// Otherwise not calling setSound makes it the default notification sound.
159157

160-
channel.setLockscreenVisibility(payload.optInt("vis", Notification.VISIBILITY_PUBLIC));
158+
channel.setLockscreenVisibility(payload.optInt("vis", Notification.VISIBILITY_PRIVATE));
161159
channel.setShowBadge(payload.optInt("bdg", 1) == 1);
162160
channel.setBypassDnd(payload.optInt("bdnd", 0) == 1);
163161

@@ -177,6 +175,17 @@ private static String createDefaultChannel(NotificationManager notificationManag
177175

178176
return DEFAULT_CHANNEL_ID;
179177
}
178+
179+
@RequiresApi(api = Build.VERSION_CODES.O)
180+
private static String createRestoreChannel(NotificationManager notificationManager) {
181+
NotificationChannel channel = new NotificationChannel(RESTORE_CHANNEL_ID,
182+
"Restored",
183+
NotificationManager.IMPORTANCE_LOW);
184+
185+
notificationManager.createNotificationChannel(channel);
186+
187+
return RESTORE_CHANNEL_ID;
188+
}
180189

181190
static void processChannelList(Context context, JSONObject payload) {
182191
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ public static class NotificationGenerationJob extends com.onesignal.Notification
9393
}
9494

9595
public static String NotificationChannelManager_createNotificationChannel(Context context, JSONObject payload) {
96-
return NotificationChannelManager.createNotificationChannel(context, payload);
96+
NotificationGenerationJob notifJob = new NotificationGenerationJob(context);
97+
notifJob.jsonPayload = payload;
98+
return NotificationChannelManager.createNotificationChannel(notifJob);
9799
}
98100

99101
public static void NotificationChannelManager_processChannelList(Context context, JSONObject jsonObject) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ public void createNotificationChannelWithALlOptions() throws Exception {
127127
assertEquals("grp_id", group.getId());
128128
assertEquals("Group Name", group.getName());
129129
assertNotNull(channel.getSound());
130-
assertFalse(channel.shouldShowLights());
130+
assertFalse(channel.shouldShowLights()); // Setting a led color should NOT override enableLights
131131
assertEquals(-65536, channel.getLightColor());
132-
assertTrue(channel.shouldVibrate()); // Setting a pattern enables vibration
132+
assertFalse(channel.shouldVibrate()); // Setting a pattern should NOT override enableVibration
133133
assertArrayEquals(new long[]{1,2,3,4}, channel.getVibrationPattern());
134134
assertEquals(NotificationManager.IMPORTANCE_MAX, channel.getImportance());
135135
assertEquals("content://settings/system/notification_sound", channel.getSound().toString());
@@ -159,7 +159,7 @@ public void useOtherChannelWhenItIsAvailable() throws Exception {
159159
}
160160

161161

162-
// Starting cold start sync tests
162+
// Start - Cold start sync tests
163163

164164
@Test
165165
public void processPayloadWithOutChannelList() throws Exception {

0 commit comments

Comments
 (0)