Skip to content

Commit 4e9ba98

Browse files
committed
Use isNotificationToDisplay flag in place of logic which sets/checks for an AndroidId of -1. This allows for generating an android ID quickly while preserving the "did display" state.
1 parent 5489900 commit 4e9ba98

File tree

4 files changed

+28
-43
lines changed

4 files changed

+28
-43
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ private static void createSummaryNotification(OSNotificationGenerationJob notifi
525525
String[] whereArgs = { group };
526526

527527
// Make sure to omit any old existing matching android ids in-case we are replacing it.
528-
if (!updateSummary && notificationJob.getAndroidId() != -1)
528+
if (!updateSummary)
529529
whereStr += " AND " + NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID + " <> " + notificationJob.getAndroidId();
530530

531531
cursor = dbHelper.query(

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ private static int processJobForDisplay(OSNotificationController notificationCon
136136

137137
processCollapseKey(notificationJob);
138138

139-
int androidNotificationId = notificationJob.getAndroidIdWithoutCreate();
139+
int androidNotificationId = notificationJob.getAndroidId();
140140
boolean doDisplay = shouldDisplayNotification(notificationJob);
141141
boolean notificationDisplayed = false;
142142

143143
if (doDisplay) {
144-
androidNotificationId = notificationJob.getAndroidId();
144+
notificationJob.setIsNotificationToDisplay(true);
145145
if (fromBackgroundLogic && OneSignal.shouldFireForegroundHandlers(notificationJob)) {
146146
notificationController.setFromBackgroundLogic(false);
147147
OneSignal.fireForegroundHandlers(notificationController);
@@ -206,10 +206,10 @@ private static void saveNotification(OSNotificationGenerationJob notificationJob
206206

207207
OneSignalDbHelper dbHelper = OneSignalDbHelper.getInstance(notificationJob.getContext());
208208

209-
// Count any notifications with duplicated android notification ids as dismissed.
210-
// -1 is used to note never displayed
209+
// When notification was displayed, count any notifications with duplicated android
210+
// notification ids as dismissed.
211211
if (notificationJob.isNotificationToDisplay()) {
212-
String whereStr = NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID + " = " + notificationJob.getAndroidIdWithoutCreate();
212+
String whereStr = NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID + " = " + notificationJob.getAndroidId();
213213

214214
ContentValues values = new ContentValues();
215215
values.put(NotificationTable.COLUMN_NAME_DISMISSED, 1);
@@ -228,7 +228,7 @@ private static void saveNotification(OSNotificationGenerationJob notificationJob
228228

229229
values.put(NotificationTable.COLUMN_NAME_OPENED, opened ? 1 : 0);
230230
if (!opened)
231-
values.put(NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID, notificationJob.getAndroidIdWithoutCreate());
231+
values.put(NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID, notificationJob.getAndroidId());
232232

233233
if (notificationJob.getTitle() != null)
234234
values.put(NotificationTable.COLUMN_NAME_TITLE, notificationJob.getTitle().toString());
@@ -253,11 +253,11 @@ private static void saveNotification(OSNotificationGenerationJob notificationJob
253253
}
254254

255255
static void markNotificationAsDismissed(OSNotificationGenerationJob notifiJob) {
256-
if (notifiJob.getAndroidIdWithoutCreate() == -1)
256+
if (!notifiJob.isNotificationToDisplay())
257257
return;
258258

259259
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "Marking restored or disabled notifications as dismissed: " + notifiJob.toString());
260-
String whereStr = NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID + " = " + notifiJob.getAndroidIdWithoutCreate();
260+
String whereStr = NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID + " = " + notifiJob.getAndroidId();
261261

262262
OneSignalDbHelper dbHelper = OneSignalDbHelper.getInstance(notifiJob.getContext());
263263

@@ -351,7 +351,7 @@ private static void processCollapseKey(OSNotificationGenerationJob notificationJ
351351

352352
if (cursor.moveToFirst()) {
353353
int androidNotificationId = cursor.getInt(cursor.getColumnIndex(NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID));
354-
notificationJob.setAndroidIdWithoutOverriding(androidNotificationId);
354+
notificationJob.getNotification().setAndroidNotificationId(androidNotificationId);
355355
}
356356

357357
cursor.close();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ private void notDisplayNotificationLogic(OSNotification originalNotification) {
113113
// This will prevent it from being restored again
114114
NotificationBundleProcessor.markNotificationAsDismissed(notificationJob);
115115
} else {
116-
// -1 is used to note never displayed
117-
notificationJob.getNotification().setAndroidNotificationId(-1);
116+
// indicate the notification job did not display
117+
notificationJob.setIsNotificationToDisplay(false);
118118
NotificationBundleProcessor.processNotification(notificationJob, true, false);
119119
OneSignal.handleNotificationReceived(notificationJob);
120120
}

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

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class OSNotificationGenerationJob {
4040
private Context context;
4141
private JSONObject jsonPayload;
4242
private boolean restoring;
43-
private boolean didGenerateId;
43+
private boolean isNotificationToDisplay;
4444

4545
private Long shownTimeStamp;
4646

@@ -91,10 +91,17 @@ JSONObject getAdditionalData() {
9191
}
9292

9393
/**
94-
* If androidNotificationId is -1 then the notification is a silent one
94+
* Determine whether this notification has been displayed.
9595
*/
9696
boolean isNotificationToDisplay() {
97-
return getAndroidIdWithoutCreate() != -1;
97+
return isNotificationToDisplay;
98+
}
99+
100+
/**
101+
* Indicate whether notification has been displayed.
102+
*/
103+
void setIsNotificationToDisplay(boolean isNotificationToDisplay) {
104+
this.isNotificationToDisplay = isNotificationToDisplay;
98105
}
99106

100107
boolean hasExtender() {
@@ -105,45 +112,22 @@ String getApiNotificationId() {
105112
return OneSignal.getNotificationIdFromFCMJson(jsonPayload);
106113
}
107114

108-
int getAndroidIdWithoutCreate() {
109-
if (didGenerateId)
110-
return -1;
111-
112-
return notification.getAndroidNotificationId();
113-
}
114-
115115
Integer getAndroidId() {
116116
return notification.getAndroidNotificationId() ;
117117
}
118118

119-
void setAndroidIdWithoutOverriding(Integer id) {
120-
if (id == null)
121-
return;
122-
123-
if (!didGenerateId)
124-
return;
125-
126-
notification.setAndroidNotificationId(id);
127-
}
128-
129119
public OSNotification getNotification() {
130120
return notification;
131121
}
132122

133123
public void setNotification(OSNotification notification) {
134-
// we determine whether an android notification id has been
135-
// generated by us only for the first notification. The assumption
136-
// is any subsequent notification updates are derived from the
137-
// initial one, therefore the didGenerateId will persist across.
138-
if(this.notification == null)
139-
{
140-
if (notification.hasNotificationId())
141-
didGenerateId = false;
124+
// If there is no android ID on the notification coming in, create one either
125+
// copying from the previous one or generating a new one.
126+
if (notification != null && !notification.hasNotificationId()) {
127+
if (this.notification != null && this.notification.hasNotificationId())
128+
notification.setAndroidNotificationId(this.notification.getAndroidNotificationId());
142129
else
143-
{
144-
didGenerateId = true;
145130
notification.setAndroidNotificationId(new SecureRandom().nextInt());
146-
}
147131
}
148132

149133
this.notification = notification;
@@ -234,6 +218,7 @@ public String toString() {
234218
return "OSNotificationGenerationJob{" +
235219
"jsonPayload=" + jsonPayload +
236220
", isRestoring=" + restoring +
221+
", isNotificationToDisplay=" + isNotificationToDisplay +
237222
", shownTimeStamp=" + shownTimeStamp +
238223
", overriddenBodyFromExtender=" + overriddenBodyFromExtender +
239224
", overriddenTitleFromExtender=" + overriddenTitleFromExtender +

0 commit comments

Comments
 (0)