Obtain contents of remote push notification received while the app is not running after user launches the application from app icon

General Tech Bugs & Fixes 2 years ago

1 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 31 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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:

  1. Remote push notification is sent to a device while the app is not running (not on background, but not running at all).
  2. 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 called
  • UNUserNotificationCenter.current().getDeliveredNotifications returns 0 notifications (although notification is visible in notification area).
  • In didFinishLaunchingWithOptions the value of launchOptions 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.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.