Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ttl and related current time logic #1528

Merged
merged 4 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private static void saveNotification(OSNotificationGenerationJob notificationJob
values.put(NotificationTable.COLUMN_NAME_MESSAGE, notificationJob.getBody().toString());

// Set expire_time
long sentTime = jsonPayload.optLong(OSNotificationController.GOOGLE_SENT_TIME_KEY, OneSignal.getTime().getCurrentThreadTimeMillis()) / 1_000L;
long sentTime = jsonPayload.optLong(OSNotificationController.GOOGLE_SENT_TIME_KEY, OneSignal.getTime().getCurrentTimeMillis()) / 1_000L;
int ttl = jsonPayload.optInt(OSNotificationController.GOOGLE_TTL_KEY, OSNotificationRestoreWorkManager.DEFAULT_TTL_IF_NOT_IN_PAYLOAD);
long expireTime = sentTime + ttl;
values.put(NotificationTable.COLUMN_NAME_EXPIRE_TIME, expireTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ protected OSNotification(OSNotification notification) {
this.title = notification.title;
this.body = notification.body;
this.additionalData = notification.additionalData;
this.smallIcon = notification.smallIcon;
this.largeIcon = notification.largeIcon;
this.bigPicture = notification.bigPicture;
this.smallIconAccentColor = notification.smallIconAccentColor;
Expand All @@ -132,6 +133,8 @@ protected OSNotification(OSNotification notification) {
this.collapseId = notification.collapseId;
this.priority = notification.priority;
this.rawPayload = notification.rawPayload;
this.sentTime = notification.sentTime;
this.ttl = notification.ttl;
}

private void initPayloadData(JSONObject currentJsonPayload) {
Expand All @@ -143,7 +146,7 @@ private void initPayloadData(JSONObject currentJsonPayload) {
return;
}

long currentTime = OneSignal.getTime().getCurrentThreadTimeMillis();
long currentTime = OneSignal.getTime().getCurrentTimeMillis();
if (currentJsonPayload.has(GOOGLE_TTL_KEY)) {
sentTime = currentJsonPayload.optLong(GOOGLE_SENT_TIME_KEY, currentTime) / 1_000;
ttl = currentJsonPayload.optInt(GOOGLE_TTL_KEY, OSNotificationRestoreWorkManager.DEFAULT_TTL_IF_NOT_IN_PAYLOAD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public boolean isNotificationWithinTTL() {
if (!useTtl)
return true;

long currentTimeInSeconds = OneSignal.getTime().getCurrentThreadTimeMillis() / 1_000;
long currentTimeInSeconds = OneSignal.getTime().getCurrentTimeMillis() / 1_000;
long sentTime = notificationJob.getNotification().getSentTime();
// If available TTL times comes in seconds, by default is 3 days in seconds
int ttl = notificationJob.getNotification().getTtl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1252,15 +1252,15 @@ public void shouldSetExpireTimeCorrectlyFromGoogleTTL() throws Exception {
@Test
@Config (sdk = 23, shadows = { ShadowGenerateNotification.class })
public void notShowNotificationPastTTL() throws Exception {
long sentTime = time.getCurrentThreadTimeMillis();
long sentTime = time.getCurrentTimeMillis();
long ttl = 60L;

Bundle bundle = getBaseNotifBundle();
bundle.putLong(OneSignalPackagePrivateHelper.GOOGLE_SENT_TIME_KEY, sentTime);
bundle.putLong(OneSignalPackagePrivateHelper.GOOGLE_TTL_KEY, ttl);

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

NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
threadAndTaskWait();
Expand All @@ -1275,7 +1275,7 @@ public void shouldSetExpireTimeCorrectlyWhenMissingFromPayload() throws Exceptio
threadAndTaskWait();

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

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

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void testNotificationProcessingAndForegroundHandler_callCompleteWithMutableNotification_displays() throws Exception {
// 1. Setup correct notification extension service class
startRemoteNotificationReceivedHandlerService(
RemoteNotificationReceivedHandler_notificationReceivedCallCompleteWithMutableNotification
.class
.getName()
);

// 2. Init OneSignal
OneSignal.setAppId("b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
OneSignal.initWithContext(blankActivity);
OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
lastForegroundNotificationReceivedEvent = notificationReceivedEvent;

// Call complete to end without waiting default 30 second timeout
notificationReceivedEvent.complete(notificationReceivedEvent.getNotification());
});
threadAndTaskWait();

blankActivityController.resume();
threadAndTaskWait();

// 3. Receive a notification in foreground
FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
threadAndTaskWait();

// 4. Make sure service was called
assertNotNull(lastServiceNotificationReceivedEvent);

// 5. Make sure foreground handler was called
assertNotNull(lastForegroundNotificationReceivedEvent);

// 6. Make sure running on main thread check is called, this is only called for showing the notification
assertTrue(ShadowGenerateNotification.isRunningOnMainThreadCheckCalled());

// 7. Check badge count to represent the notification is displayed
assertEquals(1, ShadowBadgeCountUpdater.lastCount);
}

/**
* @see #testNotificationProcessingAndForegroundHandler_callCompleteWithMutableNotification_displays
*/
public static class RemoteNotificationReceivedHandler_notificationReceivedCallCompleteWithMutableNotification implements OneSignal.OSRemoteNotificationReceivedHandler {

@Override
public void remoteNotificationReceived(final Context context, OSNotificationReceivedEvent receivedEvent) {
lastServiceNotificationReceivedEvent = receivedEvent;

OSNotification notification = receivedEvent.getNotification();
OSMutableNotification mutableNotification = notification.mutableCopy();

// Complete is called to end NotificationProcessingHandler
receivedEvent.complete(mutableNotification);
}
}

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void testNotificationWillShowInForegroundHandlerIsCallWhenReceivingNotificationInForeground() throws Exception {
Expand Down