Skip to content

Commit 5489900

Browse files
committed
Immediately generate a notification's Android ID when not set, rather than when first requested through OSNotificationGenerationJob.getAndroidId(). This ensures that OSNotification will have an Android Notification ID earlier in the notification received process, specifically before control is given to OSRemoteNotificationReceivedHandler.
1 parent 292e1db commit 5489900

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

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

Lines changed: 19 additions & 6 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 didGenerateId;
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
/**
@@ -105,24 +106,21 @@ String getApiNotificationId() {
105106
}
106107

107108
int getAndroidIdWithoutCreate() {
108-
if (!notification.hasNotificationId())
109+
if (didGenerateId)
109110
return -1;
110111

111112
return notification.getAndroidNotificationId();
112113
}
113114

114115
Integer getAndroidId() {
115-
if (!notification.hasNotificationId())
116-
notification.setAndroidNotificationId(new SecureRandom().nextInt());
117-
118116
return notification.getAndroidNotificationId() ;
119117
}
120118

121119
void setAndroidIdWithoutOverriding(Integer id) {
122120
if (id == null)
123121
return;
124122

125-
if (notification.hasNotificationId())
123+
if (!didGenerateId)
126124
return;
127125

128126
notification.setAndroidNotificationId(id);
@@ -133,6 +131,21 @@ public OSNotification getNotification() {
133131
}
134132

135133
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;
142+
else
143+
{
144+
didGenerateId = true;
145+
notification.setAndroidNotificationId(new SecureRandom().nextInt());
146+
}
147+
}
148+
136149
this.notification = notification;
137150
}
138151

0 commit comments

Comments
 (0)