Skip to content

Commit a11751e

Browse files
committed
Exception fix on cold start from notification restoring
* Fixed null exception that can happen when the app is restoring notifications when cold started.
1 parent 364827f commit a11751e

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

OneSignalSDK.jar

22 Bytes
Binary file not shown.

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ public static Bundle createInternalPayloadBundle(Bundle bundle) {
5252
}
5353

5454
public static void NotificationBundleProcessor_ProcessFromGCMIntentService(Context context, Bundle bundle, NotificationExtenderService.OverrideSettings overrideSettings) {
55-
if (overrideSettings == null)
56-
overrideSettings = new NotificationExtenderService.OverrideSettings();
57-
5855
NotificationBundleProcessor.ProcessFromGCMIntentService(context, createInternalPayloadBundle(bundle), overrideSettings);
5956
}
6057

58+
public static void NotificationBundleProcessor_ProcessFromGCMIntentService_NoWrap(Context context, Bundle bundle, NotificationExtenderService.OverrideSettings overrideSettings) {
59+
NotificationBundleProcessor.ProcessFromGCMIntentService(context, bundle, overrideSettings);
60+
}
61+
6162
public static boolean GcmBroadcastReceiver_processBundle(Context context, Bundle bundle) {
6263
return GcmBroadcastReceiver.processBundle(context, bundle);
6364
}

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import com.onesignal.NotificationExtenderService;
4444
import com.onesignal.NotificationOpenedProcessor;
4545
import com.onesignal.OSNotificationPayload;
46-
import com.onesignal.OneSignal;
4746
import com.onesignal.OneSignalDbHelper;
4847
import com.onesignal.OneSignalPackagePrivateHelper;
4948
import com.onesignal.ShadowBadgeCountUpdater;
@@ -70,9 +69,12 @@
7069
import org.robolectric.util.ServiceController;
7170

7271
import java.util.Iterator;
73-
import java.util.List;
7472
import java.util.Map;
7573

74+
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService;
75+
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService_NoWrap;
76+
import static com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle;
77+
7678
@Config(packageName = "com.onesignal.example",
7779
constants = BuildConfig.class,
7880
shadows = { ShadowRoboNotificationManager.class, ShadowOneSignalRestClient.class, ShadowBadgeCountUpdater.class },
@@ -129,23 +131,33 @@ private Intent createOpenIntent(Bundle bundle) {
129131
public void shouldSetTitleCorrectly() throws Exception {
130132
// Should use app's Title by default
131133
Bundle bundle = getBaseNotifBundle();
132-
OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
134+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
133135
Assert.assertEquals("UnitTestApp", ShadowRoboNotificationManager.lastNotif.getContentTitle());
134136
Assert.assertEquals(1, ShadowBadgeCountUpdater.lastCount);
135137

136138
// Should allow title from GCM payload.
137139
bundle = getBaseNotifBundle("UUID2");
138140
bundle.putString("title", "title123");
139-
OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
141+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
140142
Assert.assertEquals("title123", ShadowRoboNotificationManager.lastNotif.getContentTitle());
141143
Assert.assertEquals(2, ShadowBadgeCountUpdater.lastCount);
142144
}
143145

146+
@Test
147+
public void shouldProcessRestore() throws Exception {
148+
Bundle bundle = createInternalPayloadBundle(getBaseNotifBundle());
149+
bundle.putInt("android_notif_id", 0);
150+
bundle.putBoolean("restoring", true);
151+
152+
NotificationBundleProcessor_ProcessFromGCMIntentService_NoWrap(blankActivity, bundle, null);
153+
Assert.assertEquals("UnitTestApp", ShadowRoboNotificationManager.lastNotif.getContentTitle());
154+
}
155+
144156
@Test
145157
public void shouldHandleBasicNotifications() throws Exception {
146158
// Make sure the notification got posted and the content is correct.
147159
Bundle bundle = getBaseNotifBundle();
148-
OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
160+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
149161
Assert.assertEquals(notifMessage, ShadowRoboNotificationManager.lastNotif.getContentText());
150162
Assert.assertEquals(1, ShadowBadgeCountUpdater.lastCount);
151163

@@ -167,14 +179,14 @@ public void shouldHandleBasicNotifications() throws Exception {
167179
int firstNotifId = cursor.getInt(1);
168180

169181
// Should not display a duplicate notification, count should still be 1
170-
OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
182+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
171183
cursor = readableDb.query(NotificationTable.TABLE_NAME, null, null, null, null, null, null);
172184
Assert.assertEquals(1, cursor.getCount());
173185
Assert.assertEquals(0, ShadowBadgeCountUpdater.lastCount);
174186

175187
// Display a second notification
176188
bundle = getBaseNotifBundle("UUID2");
177-
OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
189+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
178190
cursor = readableDb.query(NotificationTable.TABLE_NAME, new String[] { "android_notification_id" }, "android_notification_id <> " + firstNotifId, null, null, null, null);
179191
cursor.moveToFirst();
180192
int secondNotifId = cursor.getInt(0);
@@ -186,7 +198,7 @@ public void shouldHandleBasicNotifications() throws Exception {
186198
// Should of been added for a total of 2 records now.
187199
// First opened should of been cleaned up, 1 week old non opened notification should stay, and one new record.
188200
bundle = getBaseNotifBundle("UUID3");
189-
OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
201+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
190202
cursor = readableDb.query(NotificationTable.TABLE_NAME, new String[] { "android_notification_id" }, null, null, null, null, null);
191203
Assert.assertEquals(2, cursor.getCount());
192204
Assert.assertEquals(2, ShadowBadgeCountUpdater.lastCount);
@@ -208,7 +220,7 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
208220
// Make sure the notification got posted and the content is correct.
209221
Bundle bundle = getBaseNotifBundle();
210222
bundle.putString("grp", "test1");
211-
OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
223+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
212224

213225
Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
214226
Assert.assertEquals(2, postedNotifs.size());
@@ -240,7 +252,7 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
240252
bundle.putString("alert", "Notif test 2");
241253
bundle.putString("custom", "{\"i\": \"UUID2\"}");
242254
bundle.putString("grp", "test1");
243-
OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
255+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
244256

245257
postedNotifs = ShadowRoboNotificationManager.notifications;
246258
Assert.assertEquals(2, postedNotifs.size());
@@ -275,7 +287,7 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
275287
bundle.putString("alert", "Notif test 3");
276288
bundle.putString("custom", "{\"i\": \"UUID3\"}");
277289
bundle.putString("grp", "test1");
278-
OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
290+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
279291

280292
postedNotifsIterator = postedNotifs.entrySet().iterator();
281293
postedNotification = postedNotifsIterator.next().getValue();
@@ -336,7 +348,7 @@ public void shouldFireNotificationExtenderService() throws Exception {
336348
ServiceController<NotificationExtenderServiceTest> controller = Robolectric.buildService(NotificationExtenderServiceTest.class);
337349
NotificationExtenderServiceTest service = controller.attach().create().get();
338350
Intent testIntent = new Intent(RuntimeEnvironment.application, NotificationExtenderServiceTest.class);
339-
testIntent.putExtras(OneSignalPackagePrivateHelper.createInternalPayloadBundle(getBundleWithAllOptionsSet()));
351+
testIntent.putExtras(createInternalPayloadBundle(getBundleWithAllOptionsSet()));
340352
controller.withIntent(testIntent).startCommand(0, 0);
341353

342354
OSNotificationPayload notification = service.notification;
@@ -372,15 +384,15 @@ public void shouldFireNotificationExtenderService() throws Exception {
372384

373385
// Test a basic notification without anything special.
374386
testIntent = new Intent(RuntimeEnvironment.application, NotificationExtenderServiceTest.class);
375-
testIntent.putExtras(OneSignalPackagePrivateHelper.createInternalPayloadBundle(getBaseNotifBundle()));
387+
testIntent.putExtras(createInternalPayloadBundle(getBaseNotifBundle()));
376388
controller.withIntent(testIntent).startCommand(0, 0);
377389
Assert.assertFalse(ShadowOneSignal.messages.contains("Error assigning"));
378390

379391

380392
// Test that a notification is still displayed if the developer's code in onNotificationProcessing throws an Exception.
381393
NotificationExtenderServiceTest.throwInAppCode = true;
382394
testIntent = new Intent(RuntimeEnvironment.application, NotificationExtenderServiceTest.class);
383-
testIntent.putExtras(OneSignalPackagePrivateHelper.createInternalPayloadBundle(getBaseNotifBundle("NewUUID1")));
395+
testIntent.putExtras(createInternalPayloadBundle(getBaseNotifBundle("NewUUID1")));
384396
controller.withIntent(testIntent).startCommand(0, 0);
385397

386398
Assert.assertTrue(ShadowOneSignal.messages.contains("onNotificationProcessing throw an exception"));

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ static void ProcessFromGCMIntentService(Context context, Bundle bundle, Notifica
5353
if (!restoring && OneSignal.notValidOrDuplicated(context, jsonPayload))
5454
return;
5555

56-
if (bundle.containsKey("android_notif_id"))
56+
if (bundle.containsKey("android_notif_id")) {
57+
if (overrideSettings == null)
58+
overrideSettings = new NotificationExtenderService.OverrideSettings();
5759
overrideSettings.androidNotificationId = bundle.getInt("android_notif_id");
60+
}
5861

5962
Process(context, restoring, jsonPayload, overrideSettings);
6063
} catch (JSONException e) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void init() {
160160
private static TrackGooglePurchase trackGooglePurchase;
161161
private static TrackAmazonPurchase trackAmazonPurchase;
162162

163-
public static final String VERSION = "020500";
163+
public static final String VERSION = "020501";
164164

165165
private static AdvertisingIdentifierProvider mainAdIdProvider = new AdvertisingIdProviderGPS();
166166

0 commit comments

Comments
 (0)