-
Notifications
You must be signed in to change notification settings - Fork 369
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code changes look good to me! Made one comment on a test improvement, feel free to make the fix and merge without another approval.
Reviewed 4 of 4 files at r1, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @nan-li)
OneSignalSDK/unittest/src/test/java/com/test/onesignal/GenerateNotificationRunner.java, line 1990 at r1 (raw file):
public void testNotificationProcessingAndForegroundHandler_callCompleteWithMutableNotification_displays() throws Exception { // 1. Setup correct notification extension service class startRemoteNotificationReceivedHandlerService("com.test.onesignal.GenerateNotificationRunner$" +
Recommend using a hard reference to this class so if any code refactor is done in the future this will be a compile error instead of a failed test.
startRemoteNotificationReceivedHandlerService(RemoteNotificationReceivedHandler_notificationReceivedCallCompleteWithMutableNotification.getClass().getCanonicalName());
When the NSE calls `complete` with `notification.mutableCopy()` rather than the original notification, and the foreground handler is also set, the notification will not display. This test is added to replicate that behavior. We will address the bug in the following commit to get the test to pass.
- `OSNotification(OSNotification notification)` is used as the constructor for `OSMutableNotification` - `ttl` and `sentTime` were missing from this constructor so that these values defaulted to 0, leading to some problems with determining ttl logic later. - Unrelated to what this PR is going to address, but also added `smallIcon` here since that was also missing.
When we check `isNotificationWithinTTL` to determine if we should display it, we should use `currentTimeMillis` (milliseconds since epoch) instead of `currentThreadTimeMillis` to determine the current time that will be used in the logic. The google sent_time is also in milliseconds since epoch. During some testing, `currentThreadTimeMillis` is equivalent to almost 0 seconds due to it being the time a thread has been active. While working on this, I looked at other places that used thread time inaccurately and changed to using `currentTimeMillis` instead.
These two tests now fail after moving away from using thread time. - notShowNotificationPastTTL - shouldSetExpireTimeCorrectlyWhenMissingFromPayload Modify the test code to not use thread time
528a908
to
8dd2cdc
Compare
Description
One Line Summary
Add
ttl
andsentTime
(andsmallIcon
) to an OSNotification constructor and change usages ofcurrentThreadTimeMillis
(time thread has been active) tocurrentTimeMillis
(epoch time) instead.Details
Motivation
This PR is related to some features of #1479.
The
protected OSNotification(OSNotification notification)
constructor was missingttl
andsentTime
so that when the NSE is used along with the foreground handler, and the NSE passes on amutableCopy()
, the notification that is processed by the foreground handler next hasttl
andsentTime
missing, and thus defaulted to0
, which ends up not displaying due toisNotificationWithinTTL()
check returningfalse
erroneously.Unrelated, but
smallIcon
was also missing so I added it as well.While making the above changes, I noticed that the
currentTimeInSeconds
forisNotificationWithinTTL()
would be close to zero. When we checkisNotificationWithinTTL()
to determine if we should display it, we should usecurrentTimeMillis
(milliseconds since epoch) instead ofcurrentThreadTimeMillis
(the time a thread has been active) to determine the current time that will be used in the logic. Note that thegoogle.sent_time
is also in milliseconds since epoch.While working on this, I looked at other places that used thread time inaccurately and changed to using
currentTimeMillis
instead.Scope
I think using
currentThreadTimeMillis
was not a problem in the past because it was only the backup value whengoogle.sent_time
was not in the payload.Testing
Unit testing
Added test
testNotificationProcessingAndForegroundHandler_callCompleteWithMutableNotification_displays()
and handlerRemoteNotificationReceivedHandler_notificationReceivedCallCompleteWithMutableNotification
. The test NSE callscomplete()
with amutableCopy()
of the notification that is used by the foreground handler later. We test that the notification will display.The two tests
notShowNotificationPastTTL
andshouldSetExpireTimeCorrectlyWhenMissingFromPayload
are now failing because they were using thread time. I modified these to use current non-thread time and they now pass.Manual testing
Tested using Android emulator Pixel 5 on API 30 with the demo app in the SDK. Sent notification to the device in the foreground and it didn't display before. Now it displays. Also regularly checked values of
sentTime
,ttl
andcurrentTimeInSeconds
throughout testing.Affected code checklist
Checklist
Overview
Testing
Final pass
This change is