Skip to content

Commit 4bc65aa

Browse files
authored
Merge pull request #65 from OneSignal/badge_count_sync_fix
Restore notifications
2 parents a232f24 + 06b8e4b commit 4bc65aa

23 files changed

+657
-161
lines changed

OneSignalSDK/app/src/main/java/com/onesignal/example/MainActivity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
package com.onesignal.example;
2929

3030
import android.app.Activity;
31+
import android.app.NotificationManager;
32+
import android.content.Context;
3133
import android.support.v7.app.ActionBarActivity;
3234
import android.os.Bundle;
3335
import android.util.Log;
@@ -43,6 +45,8 @@
4345
import com.onesignal.example.iap.IabHelper;
4446
import com.onesignal.example.iap.IabResult;
4547

48+
import java.util.Locale;
49+
4650

4751
public class MainActivity extends ActionBarActivity {
4852

@@ -55,6 +59,8 @@ protected void onCreate(Bundle savedInstanceState) {
5559
super.onCreate(savedInstanceState);
5660
setContentView(R.layout.activity_main);
5761

62+
Log.e("tet", Locale.getDefault().getLanguage());
63+
5864
currentActivity = this;
5965

6066
// OneSignal.setLogLevel(OneSignal.LOG_LEVEL.DEBUG, OneSignal.LOG_LEVEL.NONE);
@@ -100,6 +106,10 @@ public void onIabSetupFinished(IabResult result) {
100106
public void onSubscribeClicked(View v) {
101107
OneSignal.setSubscription(true);
102108
OneSignal.promptLocation();
109+
110+
String ns = Context.NOTIFICATION_SERVICE;
111+
NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);
112+
nMgr.cancelAll();
103113
}
104114

105115
public void onUnsubscribeClicked(View v) {

OneSignalSDK/app/src/main/java/com/onesignal/example/NotificationExtenderServiceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected boolean onNotificationProcessing(OSNotificationPayload notification) {
1515
printObject(notification);
1616
if (notification.actionButtons != null) {
1717
for(OSNotificationPayload.ActionButton button : notification.actionButtons) {
18-
System.out.println("button:");
18+
// System.out.println("button:");
1919
printObject(button);
2020
}
2121
}
@@ -39,7 +39,7 @@ private void printObject(Object obj) {
3939
String name = field.getName();
4040
try {
4141
Object value = field.get(obj);
42-
System.out.printf("Field name: %s, Field value: %s%n", name, value);
42+
// System.out.printf("Field name: %s, Field value: %s%n", name, value);
4343
} catch (Throwable t){}
4444
}
4545
}
Binary file not shown.

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,17 @@ public static JSONObject bundleAsJSONObject(Bundle bundle) {
4545
return NotificationBundleProcessor.bundleAsJSONObject(bundle);
4646
}
4747

48+
public static Bundle createInternalPayloadBundle(Bundle bundle) {
49+
Bundle retBundle = new Bundle();
50+
retBundle.putString("json_payload", OneSignalPackagePrivateHelper.bundleAsJSONObject(bundle).toString());
51+
return retBundle;
52+
}
53+
4854
public static void NotificationBundleProcessor_ProcessFromGCMIntentService(Context context, Bundle bundle, NotificationExtenderService.OverrideSettings overrideSettings) {
49-
NotificationBundleProcessor.ProcessFromGCMIntentService(context, bundle, overrideSettings);
55+
if (overrideSettings == null)
56+
overrideSettings = new NotificationExtenderService.OverrideSettings();
57+
58+
NotificationBundleProcessor.ProcessFromGCMIntentService(context, createInternalPayloadBundle(bundle), overrideSettings);
5059
}
5160

5261
public static boolean GcmBroadcastReceiver_processBundle(Context context, Bundle bundle) {

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
import org.robolectric.shadows.ShadowNotification;
3535
import org.robolectric.shadows.ShadowNotificationManager;
3636

37-
import java.util.ArrayList;
38-
import java.util.List;
37+
import java.util.LinkedHashMap;
3938

4039
import static org.robolectric.Shadows.shadowOf;
4140

@@ -55,13 +54,21 @@ public class PostedNotification {
5554
public static ShadowNotification lastNotif;
5655
public static int lastNotifId;
5756

58-
public static List<PostedNotification> notifications = new ArrayList<PostedNotification>();
57+
public static LinkedHashMap<Integer, PostedNotification> notifications = new LinkedHashMap<>();
58+
59+
public void cancelAll() {
60+
notifications.clear();
61+
}
62+
63+
public void cancel(String tag, int id) {
64+
notifications.remove(id);
65+
}
5966

6067
@Override
6168
public void notify(String tag, int id, Notification notification) {
6269
lastNotif = shadowOf(notification);
6370
lastNotifId = id;
64-
notifications.add(new PostedNotification(id, lastNotif));
71+
notifications.put(id, new PostedNotification(id, lastNotif));
6572
super.notify(tag, id, notification);
6673
}
6774

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

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929

3030
import android.app.Activity;
3131
import android.app.Notification;
32+
import android.app.NotificationManager;
33+
import android.content.Context;
3234
import android.content.Intent;
33-
import android.content.pm.ActivityInfo;
3435
import android.content.pm.ResolveInfo;
3536
import android.content.pm.ServiceInfo;
3637
import android.database.Cursor;
@@ -41,8 +42,8 @@
4142
import com.onesignal.GcmBroadcastReceiver;
4243
import com.onesignal.NotificationExtenderService;
4344
import com.onesignal.NotificationOpenedProcessor;
44-
import com.onesignal.OSNotificationDisplayedResult;
4545
import com.onesignal.OSNotificationPayload;
46+
import com.onesignal.OneSignal;
4647
import com.onesignal.OneSignalDbHelper;
4748
import com.onesignal.OneSignalPackagePrivateHelper;
4849
import com.onesignal.ShadowBadgeCountUpdater;
@@ -68,7 +69,9 @@
6869
import org.robolectric.shadows.ShadowSystemClock;
6970
import org.robolectric.util.ServiceController;
7071

72+
import java.util.Iterator;
7173
import java.util.List;
74+
import java.util.Map;
7275

7376
@Config(packageName = "com.onesignal.example",
7477
constants = BuildConfig.class,
@@ -88,15 +91,15 @@ public static void setUpClass() throws Exception {
8891

8992
@Before // Before each test
9093
public void beforeEachTest() throws Exception {
91-
ShadowRoboNotificationManager.notifications.clear();
92-
9394
// Robolectric mocks System.currentTimeMillis() to 0, we need the current real time to match our SQL records.
9495
ShadowSystemClock.setCurrentTimeMillis(System.currentTimeMillis());
9596

9697
blankActivity = Robolectric.buildActivity(BlankActivity.class).create().get();
9798
blankActivity.getApplicationInfo().name = "UnitTestApp";
9899

99100
ShadowBadgeCountUpdater.lastCount = 0;
101+
NotificationManager notificationManager = (NotificationManager)blankActivity.getSystemService(Context.NOTIFICATION_SERVICE);
102+
notificationManager.cancelAll();
100103
}
101104

102105

@@ -207,16 +210,20 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
207210
bundle.putString("grp", "test1");
208211
OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
209212

210-
List<PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
213+
Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
211214
Assert.assertEquals(2, postedNotifs.size());
212215

213216
// Test summary notification
214-
Assert.assertEquals(notifMessage, postedNotifs.get(0).notif.getContentText());
215-
Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedNotifs.get(0).notif.getRealNotification().flags & Notification.FLAG_GROUP_SUMMARY);
217+
Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
218+
PostedNotification postedNotification = postedNotifsIterator.next().getValue();
219+
220+
Assert.assertEquals(notifMessage, postedNotification.notif.getContentText());
221+
Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY,postedNotification.notif.getRealNotification().flags & Notification.FLAG_GROUP_SUMMARY);
216222

217223
// Test Android Wear notification
218-
Assert.assertEquals(notifMessage, postedNotifs.get(1).notif.getContentText());
219-
Assert.assertEquals(0, postedNotifs.get(1).notif.getRealNotification().flags & Notification.FLAG_GROUP_SUMMARY);
224+
postedNotification = postedNotifsIterator.next().getValue();
225+
Assert.assertEquals(notifMessage, postedNotification.notif.getContentText());
226+
Assert.assertEquals(0, postedNotification.notif.getRealNotification().flags & Notification.FLAG_GROUP_SUMMARY);
220227
// Badge count should only be one as only one notification is visible in the notification area.
221228
Assert.assertEquals(1, ShadowBadgeCountUpdater.lastCount);
222229

@@ -239,12 +246,15 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
239246
Assert.assertEquals(2, postedNotifs.size());
240247
Assert.assertEquals(2, ShadowBadgeCountUpdater.lastCount);
241248

242-
Assert.assertEquals("2 new messages", postedNotifs.get(0).notif.getContentText());
243-
Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedNotifs.get(0).notif.getRealNotification().flags & Notification.FLAG_GROUP_SUMMARY);
249+
postedNotifsIterator = postedNotifs.entrySet().iterator();
250+
postedNotification = postedNotifsIterator.next().getValue();
251+
Assert.assertEquals("2 new messages",postedNotification.notif.getContentText());
252+
Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedNotification.notif.getRealNotification().flags & Notification.FLAG_GROUP_SUMMARY);
244253

245254
// Test Android Wear notification
246-
Assert.assertEquals("Notif test 2", postedNotifs.get(1).notif.getContentText());
247-
Assert.assertEquals(0, postedNotifs.get(1).notif.getRealNotification().flags & Notification.FLAG_GROUP_SUMMARY);
255+
postedNotification = postedNotifsIterator.next().getValue();
256+
Assert.assertEquals("Notif test 2", postedNotification.notif.getContentText());
257+
Assert.assertEquals(0, postedNotification.notif.getRealNotification().flags & Notification.FLAG_GROUP_SUMMARY);
248258

249259

250260
// Should be 3 DB entries (summary and 2 individual)
@@ -253,7 +263,9 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
253263

254264

255265
// Open summary notification
256-
Intent intent = createOpenIntent(postedNotifs.get(0).id, bundle).putExtra("summary", "test1");
266+
postedNotifsIterator = postedNotifs.entrySet().iterator();
267+
postedNotification = postedNotifsIterator.next().getValue();
268+
Intent intent = createOpenIntent(postedNotification.id, bundle).putExtra("summary", "test1");
257269
NotificationOpenedProcessor.processFromActivity(blankActivity, intent);
258270
Assert.assertEquals(0, ShadowBadgeCountUpdater.lastCount);
259271
ShadowRoboNotificationManager.notifications.clear();
@@ -265,8 +277,10 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
265277
bundle.putString("grp", "test1");
266278
OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
267279

268-
Assert.assertEquals("Notif test 3", postedNotifs.get(0).notif.getContentText());
269-
Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedNotifs.get(0).notif.getRealNotification().flags & Notification.FLAG_GROUP_SUMMARY);
280+
postedNotifsIterator = postedNotifs.entrySet().iterator();
281+
postedNotification = postedNotifsIterator.next().getValue();
282+
Assert.assertEquals("Notif test 3", postedNotification.notif.getContentText());
283+
Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedNotification.notif.getRealNotification().flags & Notification.FLAG_GROUP_SUMMARY);
270284
Assert.assertEquals(1, ShadowBadgeCountUpdater.lastCount);
271285
cursor.close();
272286
}
@@ -290,8 +304,11 @@ public void shouldSetButtonsCorrectly() throws Exception {
290304

291305
Intent intent = Shadows.shadowOf(blankActivity).getNextStartedService();
292306
Assert.assertEquals("com.onesignal.GcmIntentService", intent.getComponent().getClassName());
293-
Assert.assertEquals(null, intent.getStringExtra("o"));
294-
JSONObject customJson = new JSONObject(intent.getStringExtra("custom"));
307+
308+
JSONObject jsonPayload = new JSONObject(intent.getStringExtra("json_payload"));
309+
310+
Assert.assertEquals(null, jsonPayload.optString("o", null));
311+
JSONObject customJson = new JSONObject(jsonPayload.optString("custom"));
295312
JSONObject additionalData = new JSONObject((customJson.getString("a")));
296313
Assert.assertEquals("id1", additionalData.getJSONArray("actionButtons").getJSONObject(0).getString("id"));
297314
}
@@ -319,7 +336,7 @@ public void shouldFireNotificationExtenderService() throws Exception {
319336
ServiceController<NotificationExtenderServiceTest> controller = Robolectric.buildService(NotificationExtenderServiceTest.class);
320337
NotificationExtenderServiceTest service = controller.attach().create().get();
321338
Intent testIntent = new Intent(RuntimeEnvironment.application, NotificationExtenderServiceTest.class);
322-
testIntent.putExtras(getBundleWithAllOptionsSet());
339+
testIntent.putExtras(OneSignalPackagePrivateHelper.createInternalPayloadBundle(getBundleWithAllOptionsSet()));
323340
controller.withIntent(testIntent).startCommand(0, 0);
324341

325342
OSNotificationPayload notification = service.notification;
@@ -355,19 +372,19 @@ public void shouldFireNotificationExtenderService() throws Exception {
355372

356373
// Test a basic notification without anything special.
357374
testIntent = new Intent(RuntimeEnvironment.application, NotificationExtenderServiceTest.class);
358-
testIntent.putExtras(getBaseNotifBundle());
375+
testIntent.putExtras(OneSignalPackagePrivateHelper.createInternalPayloadBundle(getBaseNotifBundle()));
359376
controller.withIntent(testIntent).startCommand(0, 0);
360377
Assert.assertFalse(ShadowOneSignal.messages.contains("Error assigning"));
361378

362379

363380
// Test that a notification is still displayed if the developer's code in onNotificationProcessing throws an Exception.
364381
NotificationExtenderServiceTest.throwInAppCode = true;
365382
testIntent = new Intent(RuntimeEnvironment.application, NotificationExtenderServiceTest.class);
366-
testIntent.putExtras(getBaseNotifBundle("NewUUID1"));
383+
testIntent.putExtras(OneSignalPackagePrivateHelper.createInternalPayloadBundle(getBaseNotifBundle("NewUUID1")));
367384
controller.withIntent(testIntent).startCommand(0, 0);
368385

369386
Assert.assertTrue(ShadowOneSignal.messages.contains("onNotificationProcessing throw an exception"));
370-
List<PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
387+
Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
371388
Assert.assertEquals(3, postedNotifs.size());
372389
}
373390

0 commit comments

Comments
 (0)