I was following Apple docs and some other sources. But can't find the answer.
Problem is as follows:
Conditions:
- The remote push notification the app is receiving is time sensitive, and should be delivered to the device ASAP.
- Notification contains information which is required whichever way the user launches the app (i.e. whether they click on notification or not).
Scenario:
- Remote push notification is sent to a device while the app is not running (not on background, but not running at all).
- User doesn't click on notification. Instead, user clicks on app icon.
It appears that in this case there's no way to receive notification contents upon application startup. Notification is not lost: it stays inside notification area until user clicks on it, but notification contents are not provided to the app upon app startup.
What did I try so far
I was able to get notification contents in every other scenario:
I have a handler
func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler <...>
for when the app is in foreground
When app is in background, the handler
func application( _ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: <...>
As a safeguard, I also loop through outstanding notifications like this:
func application( _ application: UIApplication, didFinishLaunchingWithOptions <...> { UNUserNotificationCenter.current() .getDeliveredNotifications { (notifications: [UNNotification]) -> Void in
And, in the same function I am checking if
launchOptions
contains any notifications.
With this combination it looks like I'm able to cover every single scenario, except for the case I outlined above. That is: when user clicks on app icon while app is not running after notification was delivered:
didReceiveRemoteNotification:fetchCompletionHandler
is not calledUNUserNotificationCenter.current().getDeliveredNotifications
returns 0 notifications (although notification is visible in notification area).- In
didFinishLaunchingWithOptions
the value oflaunchOptions
is alos nil...
This article seems confirms this behavior, but doesn't provide any solution.
I also saw this question, but although it may be that acceptable solution for OP's needs, it does not provide a direct answer to his question, since using content-available
flag changes how notification is delivered (see Configuring a Background Update Notification section on this page, and also reduces its priority, as explained in apns-priority
section of this page.
BTW if I click on notification after opening the app, the behavior is as expected, so notification is not completely lost to the app, it's just not provided on its launch.
So what am I dealing with here? some edge case, bug or maybe such behavior is intended? if so, why?
Thanks in advance.