Skip to content

Commit 6dd61b7

Browse files
authored
Merge pull request #1597 from OneSignal/generate-android-id
Fix issue where a notification's android ID is not available to OSRemoteNotificationReceivedHandler
2 parents 8c0ffe5 + 2911e58 commit 6dd61b7

File tree

4 files changed

+33
-35
lines changed

4 files changed

+33
-35
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: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class OSNotificationGenerationJob {
4040
private Context context;
4141
private JSONObject jsonPayload;
4242
private boolean restoring;
43+
private boolean isNotificationToDisplay;
4344

4445
private Long shownTimeStamp;
4546

@@ -61,7 +62,7 @@ public class OSNotificationGenerationJob {
6162
OSNotificationGenerationJob(Context context, OSNotification notification, JSONObject jsonPayload) {
6263
this.context = context;
6364
this.jsonPayload = jsonPayload;
64-
this.notification = notification;
65+
this.setNotification(notification);
6566
}
6667

6768
/**
@@ -90,10 +91,17 @@ JSONObject getAdditionalData() {
9091
}
9192

9293
/**
93-
* If androidNotificationId is -1 then the notification is a silent one
94+
* Determine whether this notification has been displayed.
9495
*/
9596
boolean isNotificationToDisplay() {
96-
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;
97105
}
98106

99107
boolean hasExtender() {
@@ -104,35 +112,24 @@ String getApiNotificationId() {
104112
return OneSignal.getNotificationIdFromFCMJson(jsonPayload);
105113
}
106114

107-
int getAndroidIdWithoutCreate() {
108-
if (!notification.hasNotificationId())
109-
return -1;
110-
111-
return notification.getAndroidNotificationId();
112-
}
113-
114115
Integer getAndroidId() {
115-
if (!notification.hasNotificationId())
116-
notification.setAndroidNotificationId(new SecureRandom().nextInt());
117-
118116
return notification.getAndroidNotificationId() ;
119117
}
120118

121-
void setAndroidIdWithoutOverriding(Integer id) {
122-
if (id == null)
123-
return;
124-
125-
if (notification.hasNotificationId())
126-
return;
127-
128-
notification.setAndroidNotificationId(id);
129-
}
130-
131119
public OSNotification getNotification() {
132120
return notification;
133121
}
134122

135123
public void setNotification(OSNotification notification) {
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());
129+
else
130+
notification.setAndroidNotificationId(new SecureRandom().nextInt());
131+
}
132+
136133
this.notification = notification;
137134
}
138135

@@ -221,6 +218,7 @@ public String toString() {
221218
return "OSNotificationGenerationJob{" +
222219
"jsonPayload=" + jsonPayload +
223220
", isRestoring=" + restoring +
221+
", isNotificationToDisplay=" + isNotificationToDisplay +
224222
", shownTimeStamp=" + shownTimeStamp +
225223
", overriddenBodyFromExtender=" + overriddenBodyFromExtender +
226224
", overriddenTitleFromExtender=" + overriddenTitleFromExtender +

0 commit comments

Comments
 (0)