diff --git a/OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationBundleProcessor.java b/OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationBundleProcessor.java index 026cc7f32e..b579451e3e 100644 --- a/OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationBundleProcessor.java +++ b/OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationBundleProcessor.java @@ -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); diff --git a/OneSignalSDK/onesignal/src/main/java/com/onesignal/OSNotification.java b/OneSignalSDK/onesignal/src/main/java/com/onesignal/OSNotification.java index e607d333c3..b2cb7082b4 100644 --- a/OneSignalSDK/onesignal/src/main/java/com/onesignal/OSNotification.java +++ b/OneSignalSDK/onesignal/src/main/java/com/onesignal/OSNotification.java @@ -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; @@ -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) { @@ -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); diff --git a/OneSignalSDK/onesignal/src/main/java/com/onesignal/OSNotificationController.java b/OneSignalSDK/onesignal/src/main/java/com/onesignal/OSNotificationController.java index 04cd0b3e9a..4b43ff95af 100644 --- a/OneSignalSDK/onesignal/src/main/java/com/onesignal/OSNotificationController.java +++ b/OneSignalSDK/onesignal/src/main/java/com/onesignal/OSNotificationController.java @@ -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(); diff --git a/OneSignalSDK/unittest/src/test/java/com/test/onesignal/GenerateNotificationRunner.java b/OneSignalSDK/unittest/src/test/java/com/test/onesignal/GenerateNotificationRunner.java index 3d85a84ca7..5365a1a566 100644 --- a/OneSignalSDK/unittest/src/test/java/com/test/onesignal/GenerateNotificationRunner.java +++ b/OneSignalSDK/unittest/src/test/java/com/test/onesignal/GenerateNotificationRunner.java @@ -1252,7 +1252,7 @@ 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(); @@ -1260,7 +1260,7 @@ public void notShowNotificationPastTTL() throws Exception { 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(); @@ -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 @@ -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 {