Skip to content

Commit fd3da86

Browse files
authored
Merge pull request #1528 from OneSignal/fix/ttl_and_current_time
Fix `ttl` and related current time logic
2 parents a04f55f + 8dd2cdc commit fd3da86

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ private static void saveNotification(OSNotificationGenerationJob notificationJob
236236
values.put(NotificationTable.COLUMN_NAME_MESSAGE, notificationJob.getBody().toString());
237237

238238
// Set expire_time
239-
long sentTime = jsonPayload.optLong(OSNotificationController.GOOGLE_SENT_TIME_KEY, OneSignal.getTime().getCurrentThreadTimeMillis()) / 1_000L;
239+
long sentTime = jsonPayload.optLong(OSNotificationController.GOOGLE_SENT_TIME_KEY, OneSignal.getTime().getCurrentTimeMillis()) / 1_000L;
240240
int ttl = jsonPayload.optInt(OSNotificationController.GOOGLE_TTL_KEY, OSNotificationRestoreWorkManager.DEFAULT_TTL_IF_NOT_IN_PAYLOAD);
241241
long expireTime = sentTime + ttl;
242242
values.put(NotificationTable.COLUMN_NAME_EXPIRE_TIME, expireTime);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ protected OSNotification(OSNotification notification) {
117117
this.title = notification.title;
118118
this.body = notification.body;
119119
this.additionalData = notification.additionalData;
120+
this.smallIcon = notification.smallIcon;
120121
this.largeIcon = notification.largeIcon;
121122
this.bigPicture = notification.bigPicture;
122123
this.smallIconAccentColor = notification.smallIconAccentColor;
@@ -132,6 +133,8 @@ protected OSNotification(OSNotification notification) {
132133
this.collapseId = notification.collapseId;
133134
this.priority = notification.priority;
134135
this.rawPayload = notification.rawPayload;
136+
this.sentTime = notification.sentTime;
137+
this.ttl = notification.ttl;
135138
}
136139

137140
private void initPayloadData(JSONObject currentJsonPayload) {
@@ -143,7 +146,7 @@ private void initPayloadData(JSONObject currentJsonPayload) {
143146
return;
144147
}
145148

146-
long currentTime = OneSignal.getTime().getCurrentThreadTimeMillis();
149+
long currentTime = OneSignal.getTime().getCurrentTimeMillis();
147150
if (currentJsonPayload.has(GOOGLE_TTL_KEY)) {
148151
sentTime = currentJsonPayload.optLong(GOOGLE_SENT_TIME_KEY, currentTime) / 1_000;
149152
ttl = currentJsonPayload.optInt(GOOGLE_TTL_KEY, OSNotificationRestoreWorkManager.DEFAULT_TTL_IF_NOT_IN_PAYLOAD);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public boolean isNotificationWithinTTL() {
125125
if (!useTtl)
126126
return true;
127127

128-
long currentTimeInSeconds = OneSignal.getTime().getCurrentThreadTimeMillis() / 1_000;
128+
long currentTimeInSeconds = OneSignal.getTime().getCurrentTimeMillis() / 1_000;
129129
long sentTime = notificationJob.getNotification().getSentTime();
130130
// If available TTL times comes in seconds, by default is 3 days in seconds
131131
int ttl = notificationJob.getNotification().getTtl();

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

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,15 +1252,15 @@ public void shouldSetExpireTimeCorrectlyFromGoogleTTL() throws Exception {
12521252
@Test
12531253
@Config (sdk = 23, shadows = { ShadowGenerateNotification.class })
12541254
public void notShowNotificationPastTTL() throws Exception {
1255-
long sentTime = time.getCurrentThreadTimeMillis();
1255+
long sentTime = time.getCurrentTimeMillis();
12561256
long ttl = 60L;
12571257

12581258
Bundle bundle = getBaseNotifBundle();
12591259
bundle.putLong(OneSignalPackagePrivateHelper.GOOGLE_SENT_TIME_KEY, sentTime);
12601260
bundle.putLong(OneSignalPackagePrivateHelper.GOOGLE_TTL_KEY, ttl);
12611261

12621262
// Go forward just past the TTL of the notification
1263-
time.advanceThreadTimeBy(ttl + 1);
1263+
time.advanceSystemTimeBy(ttl + 1);
12641264

12651265
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
12661266
threadAndTaskWait();
@@ -1275,7 +1275,7 @@ public void shouldSetExpireTimeCorrectlyWhenMissingFromPayload() throws Exceptio
12751275
threadAndTaskWait();
12761276

12771277
long expireTime = (Long)TestHelpers.getAllNotificationRecords(dbHelper).get(0).get(NotificationTable.COLUMN_NAME_EXPIRE_TIME);
1278-
assertEquals((SystemClock.currentThreadTimeMillis() / 1_000L) + 259_200, expireTime);
1278+
assertEquals((System.currentTimeMillis() / 1_000L) + 259_200, expireTime);
12791279
}
12801280

12811281
// TODO: Once we figure out the correct way to process notifications with high priority using the WorkManager
@@ -1983,6 +1983,64 @@ public void remoteNotificationReceived(Context context, OSNotificationReceivedEv
19831983
}
19841984
}
19851985

1986+
@Test
1987+
@Config(shadows = { ShadowGenerateNotification.class })
1988+
public void testNotificationProcessingAndForegroundHandler_callCompleteWithMutableNotification_displays() throws Exception {
1989+
// 1. Setup correct notification extension service class
1990+
startRemoteNotificationReceivedHandlerService(
1991+
RemoteNotificationReceivedHandler_notificationReceivedCallCompleteWithMutableNotification
1992+
.class
1993+
.getName()
1994+
);
1995+
1996+
// 2. Init OneSignal
1997+
OneSignal.setAppId("b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
1998+
OneSignal.initWithContext(blankActivity);
1999+
OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
2000+
lastForegroundNotificationReceivedEvent = notificationReceivedEvent;
2001+
2002+
// Call complete to end without waiting default 30 second timeout
2003+
notificationReceivedEvent.complete(notificationReceivedEvent.getNotification());
2004+
});
2005+
threadAndTaskWait();
2006+
2007+
blankActivityController.resume();
2008+
threadAndTaskWait();
2009+
2010+
// 3. Receive a notification in foreground
2011+
FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
2012+
threadAndTaskWait();
2013+
2014+
// 4. Make sure service was called
2015+
assertNotNull(lastServiceNotificationReceivedEvent);
2016+
2017+
// 5. Make sure foreground handler was called
2018+
assertNotNull(lastForegroundNotificationReceivedEvent);
2019+
2020+
// 6. Make sure running on main thread check is called, this is only called for showing the notification
2021+
assertTrue(ShadowGenerateNotification.isRunningOnMainThreadCheckCalled());
2022+
2023+
// 7. Check badge count to represent the notification is displayed
2024+
assertEquals(1, ShadowBadgeCountUpdater.lastCount);
2025+
}
2026+
2027+
/**
2028+
* @see #testNotificationProcessingAndForegroundHandler_callCompleteWithMutableNotification_displays
2029+
*/
2030+
public static class RemoteNotificationReceivedHandler_notificationReceivedCallCompleteWithMutableNotification implements OneSignal.OSRemoteNotificationReceivedHandler {
2031+
2032+
@Override
2033+
public void remoteNotificationReceived(final Context context, OSNotificationReceivedEvent receivedEvent) {
2034+
lastServiceNotificationReceivedEvent = receivedEvent;
2035+
2036+
OSNotification notification = receivedEvent.getNotification();
2037+
OSMutableNotification mutableNotification = notification.mutableCopy();
2038+
2039+
// Complete is called to end NotificationProcessingHandler
2040+
receivedEvent.complete(mutableNotification);
2041+
}
2042+
}
2043+
19862044
@Test
19872045
@Config(shadows = { ShadowGenerateNotification.class })
19882046
public void testNotificationWillShowInForegroundHandlerIsCallWhenReceivingNotificationInForeground() throws Exception {

0 commit comments

Comments
 (0)