Skip to content

Commit 2755fe2

Browse files
committed
Added more channel tests and corrected sync handling
1 parent 42217ef commit 2755fe2

File tree

5 files changed

+124
-26
lines changed

5 files changed

+124
-26
lines changed

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,30 @@ static String createNotificationChannel(Context context, JSONObject jsonPayload)
5757
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
5858
return DEFAULT_CHANNEL_ID;
5959

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-
jsonPayload = customJson.optJSONObject("a");
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+
// jsonPayload = customJson.optJSONObject("a");
6868

6969
NotificationManager notificationManager =
7070
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
7171

72-
// TODO: Check for oth_chnl key, if this existing and chanel is already registered use this id
73-
// to allow any channels created outside of the SDK.
72+
// Allow channels created outside the SDK
73+
if (jsonPayload.has("oth_chnl")) {
74+
String otherChannel = jsonPayload.optString("oth_chnl");
75+
if (notificationManager.getNotificationChannel(otherChannel) != null)
76+
return otherChannel;
77+
}
7478

7579
if (!jsonPayload.has("chnl"))
7680
return createDefaultChannel(notificationManager);
77-
81+
7882
try {
79-
return createChannel(context, notificationManager, jsonPayload);
83+
return createChannel(context, notificationManager, jsonPayload.optJSONObject("chnl"));
8084
} catch (JSONException e) {
8185
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Could not create notification channel due to JSON payload error!", e);
8286
}
@@ -85,9 +89,7 @@ static String createNotificationChannel(Context context, JSONObject jsonPayload)
8589
}
8690

8791
@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-
92+
private static String createChannel(Context context, NotificationManager notificationManager, JSONObject channelPayload) throws JSONException {
9193
String channel_id = channelPayload.optString("id", DEFAULT_CHANNEL_ID);
9294
// Ensure we don't try to use the system reserved id
9395
if (channel_id.equals(NotificationChannel.DEFAULT_CHANNEL_ID))
@@ -152,9 +154,6 @@ private static String createDefaultChannel(NotificationManager notificationManag
152154
return DEFAULT_CHANNEL_ID;
153155
}
154156

155-
// TODO: 1. Check JSONObject for a chnl_lst key.
156-
// 2. Create a set of new channels.
157-
// 3. Remove any other 'OS_' not defined in this payload
158157
static void processChannelList(Context context, JSONObject payload) {
159158
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
160159
return;
@@ -170,12 +169,14 @@ static void processChannelList(Context context, JSONObject payload) {
170169
int jsonArraySize = chnlList.length();
171170
for (int i = 0; i < jsonArraySize; i++) {
172171
try {
173-
sycnedChannelSet.add(createChannel(context, notificationManager, payload));
172+
sycnedChannelSet.add(createChannel(context, notificationManager, chnlList.getJSONObject(i)));
174173
} catch (JSONException e) {
175174
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Could not create notification channel due to JSON payload error!", e);
176175
}
177176
}
178-
177+
178+
// Delete old channels - Payload will include all changes for the app. Any extra OS_ ones must
179+
// have been deleted from the dashboard and should be removed.
179180
List<NotificationChannel> existingChannels = notificationManager.getNotificationChannels();
180181
for(NotificationChannel existingChannel : existingChannels) {
181182
String id = existingChannel.getId();

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
// - Google Play services is no longer starting the broadcast receiver as an order broadcast
3939
// 4. Fix bug where restored notifications are vibrating and playing the sound again.
4040
// - 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+
4143

4244
package com.onesignal;
4345

@@ -201,11 +203,12 @@ public void init() {
201203
private static TrackGooglePurchase trackGooglePurchase;
202204
private static TrackAmazonPurchase trackAmazonPurchase;
203205

204-
public static final String VERSION = "030508";
206+
public static final String VERSION = "030600";
205207

206208
private static AdvertisingIdentifierProvider mainAdIdProvider = new AdvertisingIdProviderGPS();
207209

208210
private static int deviceType;
211+
@SuppressWarnings("WeakerAccess")
209212
public static String sdkType = "native";
210213

211214
private static OSUtils osUtils;
@@ -534,6 +537,8 @@ void onSuccess(String response) {
534537
mEnterp = responseJson.optBoolean("enterp", false);
535538

536539
awl = responseJson.getJSONObject("awl_list");
540+
541+
NotificationChannelManager.processChannelList(appContext, responseJson);
537542
} catch (Throwable t) {
538543
t.printStackTrace();
539544
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import android.os.Looper;
99
import android.support.v4.app.*;
1010

11+
import com.onesignal.example.BlankActivity;
12+
1113
import org.json.JSONObject;
1214
import org.robolectric.util.Scheduler;
1315

@@ -93,9 +95,10 @@ public static class NotificationGenerationJob extends com.onesignal.Notification
9395
public static String NotificationChannelManager_createNotificationChannel(Context context, JSONObject payload) {
9496
return NotificationChannelManager.createNotificationChannel(context, payload);
9597
}
96-
97-
// public interface BundleCompat<T> extends com.onesignal.BundleCompat {}
9898

99+
public static void NotificationChannelManager_processChannelList(Context context, JSONObject jsonObject) {
100+
NotificationChannelManager.processChannelList(context, jsonObject);
101+
}
99102

100103
public static void OneSignalRestClientPublic_getSync(final String url, final OneSignalRestClient.ResponseHandler responseHandler) {
101104
OneSignalRestClient.getSync(url, responseHandler);

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

Lines changed: 4 additions & 3 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
@@ -40,7 +40,7 @@
4040

4141
import static org.robolectric.Shadows.shadowOf;
4242

43-
@Implements(NotificationManager.class)
43+
@Implements(value = NotificationManager.class, looseSignatures = true)
4444
public class ShadowRoboNotificationManager extends ShadowNotificationManager {
4545

4646
public class PostedNotification {
@@ -95,11 +95,12 @@ public void notify(String tag, int id, Notification notification) {
9595
public static NotificationChannel lastChannel;
9696
public void createNotificationChannel(NotificationChannel channel) {
9797
lastChannel = channel;
98+
super.createNotificationChannel((Object)channel);
9899
}
99100

100101
public static NotificationChannelGroup lastChannelGroup;
101102
public void createNotificationChannelGroup(NotificationChannelGroup group) {
102103
lastChannelGroup = group;
104+
super.createNotificationChannelGroup((Object)group);
103105
}
104-
105106
}

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.app.NotificationChannel;
55
import android.app.NotificationChannelGroup;
66
import android.app.NotificationManager;
7+
import android.content.Context;
78

89
import com.onesignal.ShadowOSUtils;
910
import com.onesignal.ShadowRoboNotificationManager;
@@ -22,11 +23,13 @@
2223
import org.robolectric.shadows.ShadowLog;
2324

2425
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationChannelManager_createNotificationChannel;
26+
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationChannelManager_processChannelList;
2527
import static org.junit.Assert.assertFalse;
2628
import static junit.framework.TestCase.assertTrue;
2729
import static org.junit.Assert.assertArrayEquals;
2830
import static org.junit.Assert.assertEquals;
2931
import static org.junit.Assert.assertNotNull;
32+
import static org.junit.Assert.assertNull;
3033

3134
@Config(packageName = "com.onesignal.example",
3235
shadows = {
@@ -123,4 +126,89 @@ public void createNotificationChannelWithALlOptionsl() throws Exception {
123126
assertTrue(channel.canShowBadge());
124127
assertTrue(channel.canBypassDnd());
125128
}
129+
130+
@Test
131+
public void useOtherChannelWhenItIsAvailable() throws Exception {
132+
JSONObject payload = new JSONObject();
133+
payload.put("oth_chnl", "existing_id");
134+
135+
JSONObject chnl = new JSONObject();
136+
chnl.put("id", "test_id");
137+
payload.put("chnl", chnl);
138+
139+
String ret = NotificationChannelManager_createNotificationChannel(blankActivity, payload);
140+
141+
// Should create and use the payload type as the "existing_id" didn't exist.
142+
assertEquals("test_id", ret);
143+
144+
// Create the missing channel and using the same payload we should use this existing_id now.
145+
createChannel("existing_id");
146+
ret = NotificationChannelManager_createNotificationChannel(blankActivity, payload);
147+
assertEquals("existing_id", ret);
148+
}
149+
150+
@Test
151+
public void processPayloadWithOutChannelList() throws Exception {
152+
createChannel("local_existing_id");
153+
createChannel("OS_existing_id");
154+
155+
NotificationChannelManager_processChannelList(blankActivity, new JSONObject());
156+
157+
assertNotNull(getChannel("local_existing_id"));
158+
assertNotNull(getChannel("OS_existing_id"));
159+
}
160+
161+
@Test
162+
public void processPayloadCreatingNewChannel() throws Exception {
163+
createChannel("local_existing_id");
164+
165+
JSONArray channelList = new JSONArray();
166+
JSONObject channelItem = new JSONObject();
167+
168+
channelItem.put("id", "OS_id1");
169+
170+
channelList.put(channelItem);
171+
JSONObject payload = new JSONObject();
172+
payload.put("chnl_lst", channelList);
173+
174+
NotificationChannelManager_processChannelList(blankActivity, payload);
175+
176+
assertNotNull(getChannel("local_existing_id"));
177+
assertNotNull(getChannel("OS_id1"));
178+
}
179+
180+
181+
@Test
182+
public void processPayloadDeletingOldChannel() throws Exception {
183+
createChannel("local_existing_id");
184+
createChannel("OS_existing_id");
185+
186+
JSONArray channelList = new JSONArray();
187+
JSONObject channelItem = new JSONObject();
188+
189+
channelItem.put("id", "OS_id1");
190+
191+
channelList.put(channelItem);
192+
JSONObject payload = new JSONObject();
193+
payload.put("chnl_lst", channelList);
194+
195+
NotificationChannelManager_processChannelList(blankActivity, payload);
196+
197+
assertNotNull(getChannel("local_existing_id"));
198+
assertNull(getChannel("OS_existing_id"));
199+
assertNotNull(getChannel("OS_id1"));
200+
}
201+
202+
private NotificationChannel getChannel(String id) {
203+
NotificationManager notificationManager =
204+
(NotificationManager)blankActivity.getSystemService(Context.NOTIFICATION_SERVICE);
205+
return notificationManager.getNotificationChannel(id);
206+
}
207+
208+
private void createChannel(String id) {
209+
NotificationManager notificationManager =
210+
(NotificationManager)blankActivity.getSystemService(Context.NOTIFICATION_SERVICE);
211+
NotificationChannel channel = new NotificationChannel(id,"name", NotificationManager.IMPORTANCE_DEFAULT);
212+
notificationManager.createNotificationChannel(channel);
213+
}
126214
}

0 commit comments

Comments
 (0)