Skip to content

Commit d7ad1d1

Browse files
committed
Android O crash fix when processing a high priority message
* Fixed Bundle type cast error when a High priority notification with a remote resource was being processed.
1 parent 44fa1e9 commit d7ad1d1

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ private static boolean isGcmMessage(Intent intent) {
5858

5959
@Override
6060
public void onReceive(Context context, Intent intent) {
61-
// Google Play services started sending an extra non-ordered broadcast with the mBundle:
62-
// { "COM": "RST_FULL", "from": "google.com/iid" }
63-
// Result codes are not valid with non-ordered broadcasts so omit it to prevent errors to the logcat.
61+
// Do not process token update messages here.
62+
// They are also non-ordered broadcasts.
6463
Bundle bundle = intent.getExtras();
6564
if (bundle == null || "google.com/iid".equals(bundle.getString("from")))
6665
return;
@@ -120,18 +119,19 @@ private static ProcessedBundleResult processOrderBroadcast(Context context, Inte
120119
}
121120

122121
private static void startGCMService(Context context, Bundle bundle) {
123-
BundleCompat taskExtras = BundleCompatFactory.getInstance();
124-
taskExtras.putString("json_payload", NotificationBundleProcessor.bundleAsJSONObject(bundle).toString());
125-
taskExtras.putLong("timestamp", System.currentTimeMillis() / 1000L);
122+
BundleCompat taskExtras;
126123

127124
// If no remote resources have to be downloaded don't create a job which could add some delay.
128125
if (!NotificationBundleProcessor.hasRemoteResource(bundle)) {
126+
taskExtras = setCompatBundleForServer(bundle, BundleCompatFactory.getInstance());
129127
NotificationBundleProcessor.ProcessFromGCMIntentService(context, taskExtras, null);
130128
return;
131129
}
132130

133131
boolean isHighPriority = Integer.parseInt(bundle.getString("pri", "0")) > 9;
134132
if (!isHighPriority && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
133+
taskExtras = setCompatBundleForServer(bundle, BundleCompatFactory.getInstance());
134+
135135
ComponentName componentName = new ComponentName(context.getPackageName(),
136136
GcmIntentJobService.class.getName());
137137
Random random = new Random();
@@ -150,10 +150,18 @@ private static void startGCMService(Context context, Bundle bundle) {
150150
else {
151151
ComponentName componentName = new ComponentName(context.getPackageName(),
152152
GcmIntentService.class.getName());
153+
154+
taskExtras = setCompatBundleForServer(bundle, new BundleCompatBundle());
153155
Intent intentForService = new Intent()
154156
.replaceExtras((Bundle)taskExtras.getBundle())
155157
.setComponent(componentName);
156158
startWakefulService(context, intentForService);
157159
}
158160
}
161+
162+
private static BundleCompat setCompatBundleForServer(Bundle bundle, BundleCompat taskExtras) {
163+
taskExtras.putString("json_payload", NotificationBundleProcessor.bundleAsJSONObject(bundle).toString());
164+
taskExtras.putLong("timestamp", System.currentTimeMillis() / 1000L);
165+
return taskExtras;
166+
}
159167
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,26 @@ public void shouldPreventOtherGCMReceiversWhenSettingEnabled() throws Exception
707707
Assert.assertTrue(ShadowGcmBroadcastReceiver.calledAbortBroadcast);
708708
}
709709

710+
@Test
711+
@Config(shadows = {ShadowGcmBroadcastReceiver.class}, sdk = 26)
712+
public void shouldStartGCMServiceOnAndroidOWhenPriorityIsHighAndContainsRemoteResource() throws Exception {
713+
714+
Intent intentGcm = new Intent();
715+
intentGcm.setAction("com.google.android.c2dm.intent.RECEIVE");
716+
intentGcm.putExtra("message_type", "gcm");
717+
Bundle bundle = getBaseNotifBundle();
718+
bundle.putString("pri", "10");
719+
bundle.putString("licon", "http://domain.com/image.jpg");
720+
bundle.putString("o", "[{\"n\": \"text1\", \"i\": \"id1\"}]");
721+
intentGcm.putExtras(bundle);
722+
723+
GcmBroadcastReceiver gcmBroadcastReceiver = new GcmBroadcastReceiver();
724+
gcmBroadcastReceiver.onReceive(blankActivity, intentGcm);
725+
726+
Intent intent = Shadows.shadowOf(blankActivity).getNextStartedService();
727+
Assert.assertEquals(GcmIntentService.class.getName(), intent.getComponent().getClassName());
728+
}
729+
710730
private OSNotification lastNotificationReceived;
711731
@Test
712732
public void shouldStillFireReceivedHandlerWhenNotificationExtenderServiceIsUsed() throws Exception {

0 commit comments

Comments
 (0)