Skip to content

Commit 9e49642

Browse files
committed
Added new cancelGroupedNotifications method
1 parent 546de28 commit 9e49642

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
lines changed

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,74 @@ public static void cancelNotification(int id) {
14971497
NotificationManager notificationManager = (NotificationManager)appContext.getSystemService(Context.NOTIFICATION_SERVICE);
14981498
notificationManager.cancel(id);
14991499
}
1500+
1501+
1502+
public static void cancelGroupedNotifications(String group) {
1503+
if (appContext == null) {
1504+
Log(LOG_LEVEL.ERROR, "OneSignal.init has not been called. Could not clear notifications part of group " + group);
1505+
return;
1506+
}
1507+
1508+
NotificationManager notificationManager = (NotificationManager)appContext.getSystemService(Context.NOTIFICATION_SERVICE);
1509+
1510+
OneSignalDbHelper dbHelper = OneSignalDbHelper.getInstance(appContext);
1511+
Cursor cursor = null;
1512+
1513+
try {
1514+
SQLiteDatabase readableDb = dbHelper.getReadableDbWithRetries();
1515+
1516+
String[] retColumn = { NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID };
1517+
1518+
String whereStr = NotificationTable.COLUMN_NAME_GROUP_ID + " = ? AND " +
1519+
NotificationTable.COLUMN_NAME_DISMISSED + " = 0 AND " +
1520+
NotificationTable.COLUMN_NAME_OPENED + " = 0";
1521+
String[] whereArgs = { group };
1522+
1523+
cursor = readableDb.query(
1524+
NotificationTable.TABLE_NAME,
1525+
retColumn,
1526+
whereStr,
1527+
whereArgs,
1528+
null, null, null);
1529+
1530+
while (cursor.moveToNext()) {
1531+
int notifId = cursor.getInt(cursor.getColumnIndex(NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID));
1532+
if (notifId != -1)
1533+
notificationManager.cancel(notifId);
1534+
}
1535+
}
1536+
catch (Throwable t) {
1537+
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Error getting android notifications part of group: " + group, t);
1538+
}
1539+
finally {
1540+
if (cursor != null && !cursor.isClosed())
1541+
cursor.close();
1542+
}
1543+
1544+
SQLiteDatabase writableDb = null;
1545+
try {
1546+
writableDb = dbHelper.getWritableDbWithRetries();
1547+
writableDb.beginTransaction();
1548+
1549+
String whereStr = NotificationTable.COLUMN_NAME_GROUP_ID + " = ? AND " +
1550+
NotificationTable.COLUMN_NAME_OPENED + " = 0 AND " +
1551+
NotificationTable.COLUMN_NAME_DISMISSED + " = 0";
1552+
String[] whereArgs = { group };
1553+
1554+
ContentValues values = new ContentValues();
1555+
values.put(NotificationTable.COLUMN_NAME_DISMISSED, 1);
1556+
1557+
writableDb.update(NotificationTable.TABLE_NAME, values, whereStr, whereArgs);
1558+
BadgeCountUpdater.update(writableDb, appContext);
1559+
1560+
writableDb.setTransactionSuccessful();
1561+
} catch (Throwable t) {
1562+
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Error marking a notifications with group " + group + " as dismissed! ", t);
1563+
} finally {
1564+
if (writableDb != null)
1565+
writableDb.endTransaction();
1566+
}
1567+
}
15001568

15011569
public static void removeNotificationOpenedHandler() {
15021570
getCurrentOrNewInitBuilder().mNotificationOpenedHandler = null;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static void setup(Context context, String appId, String userId, String adId) {
5555
if (OneSignal.mEnterp)
5656
return;
5757

58-
if (userId == null || adId == null || adId.equals("OptedOut"))
58+
if (userId == null)
5959
return;
6060

6161
try {
@@ -64,7 +64,11 @@ static void setup(Context context, String appId, String userId, String adId) {
6464
return;
6565
}
6666

67-
String params = "?app_id=" + appId + "&user_id=" + userId + "&ad_id=" + adId + "&cbs_id=" + new Random().nextInt(Integer.MAX_VALUE);
67+
String params = "?app_id=" + appId + "&user_id=" + userId;
68+
if (adId != null)
69+
params += "&ad_id=" + adId;
70+
params += "&cbs_id=" + new Random().nextInt(Integer.MAX_VALUE);
71+
6872
CustomTabsServiceConnection connection = new OneSignalCustomTabsServiceConnection(context, params);
6973
opened = CustomTabsClient.bindCustomTabsService(context, "com.android.chrome", connection);
7074
}

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public void shouldSetCorrectNumberOfButtonsOnSummaryNotification() throws Except
233233
OneSignal.init(blankActivity, "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
234234
threadAndTaskWait();
235235

236-
// Setup - Display 3 notifications that will be grouped together.
236+
// Setup - Display a single notification with a grouped.
237237
Bundle bundle = getBaseNotifBundle("UUID1");
238238
bundle.putString("grp", "test1");
239239
bundle.putString("custom", "{\"i\": \"some_UUID\", \"a\": {\"actionButtons\": [{\"text\": \"test\"} ]}}");
@@ -248,6 +248,34 @@ public void shouldSetCorrectNumberOfButtonsOnSummaryNotification() throws Except
248248
Assert.assertEquals(1, postedSummaryNotification.notif.actions.length);
249249
}
250250

251+
252+
253+
@Test
254+
public void shouldCancelAllNotificationsPartOfAGroup() throws Exception {
255+
// Setup - Init
256+
OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification);
257+
OneSignal.init(blankActivity, "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
258+
threadAndTaskWait();
259+
260+
// Setup - Display 3 notifications, 2 of which that will be grouped together.
261+
Bundle bundle = getBaseNotifBundle("UUID0");
262+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
263+
264+
bundle = getBaseNotifBundle("UUID1");
265+
bundle.putString("grp", "test1");
266+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
267+
bundle = getBaseNotifBundle("UUID2");
268+
bundle.putString("grp", "test1");
269+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
270+
271+
Assert.assertEquals(4, ShadowRoboNotificationManager.notifications.size());
272+
273+
OneSignal.cancelGroupedNotifications("test1");
274+
Assert.assertEquals(1, ShadowRoboNotificationManager.notifications.size());
275+
}
276+
277+
278+
251279
@Test
252280
@Config(shadows = {ShadowNotificationRestorer.class})
253281
public void shouldCancelNotificationAndUpdateSummary() throws Exception {

0 commit comments

Comments
 (0)