Skip to content

Commit 7ffb98f

Browse files
committed
Fixed some properties not assigning on NotificationExtenderService
1 parent 3f96c51 commit 7ffb98f

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

OneSignalSDK.jar

319 Bytes
Binary file not shown.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.onesignal.OneSignalDbHelper;
4646
import com.onesignal.OneSignalPackagePrivateHelper;
4747
import com.onesignal.ShadowBadgeCountUpdater;
48+
import com.onesignal.ShadowOneSignal;
4849
import com.onesignal.ShadowOneSignalRestClient;
4950
import com.onesignal.ShadowRoboNotificationManager;
5051
import com.onesignal.ShadowRoboNotificationManager.PostedNotification;
@@ -280,6 +281,7 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
280281
}
281282

282283
@Test
284+
@Config(shadows = {ShadowOneSignal.class})
283285
public void shouldFireNotificationExtenderService() throws Exception {
284286
Bundle bundle = getBaseNotifBundle();
285287

@@ -332,6 +334,24 @@ public void shouldFireNotificationExtenderService() throws Exception {
332334
Assert.assertEquals("nValue", additionalData.getJSONObject("nested").getString("nKey"));
333335

334336
Assert.assertNotSame(-1, service.notificationId);
337+
338+
339+
// Test a basic notification without anything special.
340+
testIntent = new Intent(RuntimeEnvironment.application, NotificationExtenderServiceTest.class);
341+
testIntent.putExtras(getBaseNotifBundle());
342+
controller.withIntent(testIntent).startCommand(0, 0);
343+
Assert.assertFalse(ShadowOneSignal.messages.contains("Error assigning"));
344+
345+
346+
// Test that a notification is still displayed if the developer's code in onNotificationProcessing throws an Exception.
347+
NotificationExtenderServiceTest.throwInAppCode = true;
348+
testIntent = new Intent(RuntimeEnvironment.application, NotificationExtenderServiceTest.class);
349+
testIntent.putExtras(getBaseNotifBundle("NewUUID1"));
350+
controller.withIntent(testIntent).startCommand(0, 0);
351+
352+
Assert.assertTrue(ShadowOneSignal.messages.contains("onNotificationProcessing throw an exception"));
353+
List<PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
354+
Assert.assertEquals(3, postedNotifs.size());
335355
}
336356

337357
private static Bundle getBundleWithAllOptionsSet() {
@@ -372,6 +392,7 @@ private static Bundle getBundleWithAllOptionsSet() {
372392
public static class NotificationExtenderServiceTest extends NotificationExtenderService {
373393
public OSNotificationPayload notification;
374394
public int notificationId = -1;
395+
public static boolean throwInAppCode;
375396

376397
// Override onStart to manually call onHandleIntent on the main thread.
377398
@Override
@@ -382,6 +403,9 @@ public void onStart(Intent intent, int startId) {
382403

383404
@Override
384405
protected boolean onNotificationProcessing(OSNotificationPayload notification) {
406+
if (throwInAppCode)
407+
throw new NullPointerException();
408+
385409
this.notification = notification;
386410
notificationId = displayNotification(new OverrideSettings()).notificationId;
387411

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected final OSNotificationDisplayedResult displayNotification(OverrideSettin
8181
if (osNotificationDisplayedResult != null || overrideSettings == null)
8282
return null;
8383

84-
OSNotificationDisplayedResult osNotificationDisplayedResult = new OSNotificationDisplayedResult();
84+
osNotificationDisplayedResult = new OSNotificationDisplayedResult();
8585
osNotificationDisplayedResult.notificationId = NotificationBundleProcessor.Process(this, currentExtras, overrideSettings);
8686
return osNotificationDisplayedResult;
8787
}
@@ -119,7 +119,9 @@ private void processIntent(Intent intent) {
119119
notification.groupMessage = currentExtras.getString("grp_msg");
120120
notification.backgroundColor = currentExtras.getString("bgac");
121121
notification.ledColor = currentExtras.getString("ledc");
122-
notification.visibility = Integer.parseInt(currentExtras.getString("vis"));
122+
String visibility = currentExtras.getString("vis");
123+
if (visibility != null)
124+
notification.visibility = Integer.parseInt(visibility);
123125
notification.backgroundData = "1".equals(currentExtras.getString("bgn"));
124126
notification.fromProjectNumber = currentExtras.getString("from");
125127

@@ -143,7 +145,17 @@ private void processIntent(Intent intent) {
143145
}
144146

145147
osNotificationDisplayedResult = null;
146-
boolean developerProcessed = onNotificationProcessing(notification);
148+
boolean developerProcessed = false;
149+
try {
150+
developerProcessed = onNotificationProcessing(notification);
151+
}
152+
catch (Throwable t) {
153+
//noinspection ConstantConditions - displayNotification might have been called by the developer
154+
if (osNotificationDisplayedResult == null)
155+
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "onNotificationProcessing throw an exception. Displaying normal OneSignal notification. ", t);
156+
else
157+
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "onNotificationProcessing throw an exception. Extended notification displayed but custom processing did not finish.", t);
158+
}
147159

148160
// If developer did not call displayNotification from onNotificationProcessing
149161
if (osNotificationDisplayedResult == null) {

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 = "020400";
163+
public static final String VERSION = "020401";
164164

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

0 commit comments

Comments
 (0)