3
3
import android .app .NotificationManager ;
4
4
import android .content .ContentValues ;
5
5
import android .content .Context ;
6
+ import android .content .Intent ;
6
7
import android .database .Cursor ;
7
8
import android .database .sqlite .SQLiteDatabase ;
8
9
@@ -34,64 +35,66 @@ static void updatePossibleDependentSummaryOnDismiss(Context context, SQLiteDatab
34
35
35
36
// Called from an opened / dismissed / cancel event of a single notification to update it's parent the summary notification.
36
37
static void updateSummaryNotificationAfterChildRemoved (Context context , SQLiteDatabase writableDb , String group , boolean dismissed ) {
38
+ Cursor cursor = null ;
39
+ try {
40
+ cursor = internalUpdateSummaryNotificationAfterChildRemoved (context , writableDb , group , dismissed );
41
+ } catch (Throwable t ) {
42
+ OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "Error running updateSummaryNotificationAfterChildRemoved!" , t );
43
+ } finally {
44
+ if (cursor != null && !cursor .isClosed ())
45
+ cursor .close ();
46
+ }
47
+ }
48
+
49
+ private static Cursor internalUpdateSummaryNotificationAfterChildRemoved (Context context , SQLiteDatabase writableDb , String group , boolean dismissed ) {
37
50
Cursor cursor = writableDb .query (
38
51
NotificationTable .TABLE_NAME ,
39
52
new String [] { NotificationTable .COLUMN_NAME_ANDROID_NOTIFICATION_ID , // return columns
40
- NotificationTable .COLUMN_NAME_CREATED_TIME },
53
+ NotificationTable .COLUMN_NAME_CREATED_TIME },
41
54
NotificationTable .COLUMN_NAME_GROUP_ID + " = ? AND " + // Where String
42
55
NotificationTable .COLUMN_NAME_DISMISSED + " = 0 AND " +
43
56
NotificationTable .COLUMN_NAME_OPENED + " = 0 AND " +
44
57
NotificationTable .COLUMN_NAME_IS_SUMMARY + " = 0" ,
45
58
new String [] { group }, // whereArgs
46
59
null , null ,
47
60
NotificationTable ._ID + " DESC" ); // sort order, new to old);
48
-
61
+
49
62
int notifsInGroup = cursor .getCount ();
50
63
51
64
// If all individual notifications consumed
52
65
// - Remove summary notification from the shade.
53
66
// - Mark summary notification as consumed.
54
67
if (notifsInGroup == 0 ) {
55
68
cursor .close ();
56
-
57
- // Get the Android Notification ID of the summary notification
58
- cursor = writableDb .query (
59
- NotificationTable .TABLE_NAME ,
60
- new String [] { NotificationTable .COLUMN_NAME_ANDROID_NOTIFICATION_ID }, // retColumn
61
- NotificationTable .COLUMN_NAME_GROUP_ID + " = ? AND " + // Where String
62
- NotificationTable .COLUMN_NAME_DISMISSED + " = 0 AND " +
63
- NotificationTable .COLUMN_NAME_OPENED + " = 0 AND " +
64
- NotificationTable .COLUMN_NAME_IS_SUMMARY + " = 1" ,
65
- new String [] { group }, // whereArgs
66
- null , null , null );
67
-
68
- boolean hasRecord = cursor .moveToFirst ();
69
- if (!hasRecord )
70
- return ;
71
- int androidNotifId = cursor .getInt (cursor .getColumnIndex (NotificationTable .COLUMN_NAME_ANDROID_NOTIFICATION_ID ));
72
- cursor .close ();
73
-
69
+
70
+ Integer androidNotifId = getSummaryNotificationId (writableDb , group );
71
+ if (androidNotifId == null )
72
+ return cursor ;
73
+
74
74
// Remove the summary notification from the shade.
75
75
NotificationManager notificationManager = (NotificationManager )context .getSystemService (Context .NOTIFICATION_SERVICE );
76
76
notificationManager .cancel (androidNotifId );
77
-
77
+
78
78
// Mark the summary notification as opened or dismissed.
79
79
ContentValues values = new ContentValues ();
80
80
values .put (dismissed ? NotificationTable .COLUMN_NAME_DISMISSED : NotificationTable .COLUMN_NAME_OPENED , 1 );
81
81
writableDb .update (NotificationTable .TABLE_NAME ,
82
- values ,
83
- NotificationTable .COLUMN_NAME_ANDROID_NOTIFICATION_ID + " = " + androidNotifId ,
84
- null );
85
- return ;
82
+ values ,
83
+ NotificationTable .COLUMN_NAME_ANDROID_NOTIFICATION_ID + " = " + androidNotifId ,
84
+ null );
85
+ return cursor ;
86
86
}
87
-
87
+
88
88
// Only a single notification now in the group
89
89
// - Need to recreate a summary notification so it looks like a normal notifications since we
90
90
// only have one notification now.
91
91
if (notifsInGroup == 1 ) {
92
92
cursor .close ();
93
+ Integer androidNotifId = getSummaryNotificationId (writableDb , group );
94
+ if (androidNotifId == null )
95
+ return cursor ;
93
96
restoreSummary (context , group );
94
- return ;
97
+ return cursor ;
95
98
}
96
99
97
100
// 2 or more still left in the group
@@ -102,17 +105,23 @@ static void updateSummaryNotificationAfterChildRemoved(Context context, SQLiteDa
102
105
cursor .moveToFirst ();
103
106
Long datetime = cursor .getLong (cursor .getColumnIndex (NotificationTable .COLUMN_NAME_CREATED_TIME ));
104
107
cursor .close ();
108
+
109
+ Integer androidNotifId = getSummaryNotificationId (writableDb , group );
110
+ if (androidNotifId == null )
111
+ return cursor ;
105
112
106
113
NotificationGenerationJob notifJob = new NotificationGenerationJob (context );
107
114
notifJob .restoring = true ;
108
115
notifJob .shownTimeStamp = datetime ;
109
-
116
+
110
117
JSONObject payload = new JSONObject ();
111
118
payload .put ("grp" , group );
112
119
notifJob .jsonPayload = payload ;
113
-
120
+
114
121
GenerateNotification .updateSummaryNotification (notifJob );
115
122
} catch (JSONException e ) {}
123
+
124
+ return cursor ;
116
125
}
117
126
118
127
private static void restoreSummary (Context context , String group ) {
@@ -145,4 +154,36 @@ private static void restoreSummary(Context context, String group) {
145
154
cursor .close ();
146
155
}
147
156
}
157
+
158
+ private static Integer getSummaryNotificationId (SQLiteDatabase writableDb , String group ) {
159
+ Integer androidNotifId = null ;
160
+ Cursor cursor = null ;
161
+ try {
162
+ // Get the Android Notification ID of the summary notification
163
+ cursor = writableDb .query (
164
+ NotificationTable .TABLE_NAME ,
165
+ new String [] { NotificationTable .COLUMN_NAME_ANDROID_NOTIFICATION_ID }, // retColumn
166
+ NotificationTable .COLUMN_NAME_GROUP_ID + " = ? AND " + // Where String
167
+ NotificationTable .COLUMN_NAME_DISMISSED + " = 0 AND " +
168
+ NotificationTable .COLUMN_NAME_OPENED + " = 0 AND " +
169
+ NotificationTable .COLUMN_NAME_IS_SUMMARY + " = 1" ,
170
+ new String [] { group }, // whereArgs
171
+ null , null , null );
172
+
173
+ boolean hasRecord = cursor .moveToFirst ();
174
+ if (!hasRecord ) {
175
+ cursor .close ();
176
+ return null ;
177
+ }
178
+ androidNotifId = cursor .getInt (cursor .getColumnIndex (NotificationTable .COLUMN_NAME_ANDROID_NOTIFICATION_ID ));
179
+ cursor .close ();
180
+ } catch (Throwable t ) {
181
+ OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "Error getting android notification id for summary notification group: " + group , t );
182
+ } finally {
183
+ if (cursor != null && !cursor .isClosed ())
184
+ cursor .close ();
185
+ }
186
+
187
+ return androidNotifId ;
188
+ }
148
189
}
0 commit comments