Push notification in Background works fine when App runs by Xcode, but does not work when App runs by itself.

Hello,

I have a very strange synptom like described in the title.


My app is using push notification and it works fine when I run the app from Xcode RUN.


Now, I use my iPhone and tap the app to launch it and let it go to background.

While the app is in background and the app received push notification, the bannar is shown on the iPhone display. I tap the banner then app is launched, but the following function is not called.


func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)


I have no idea why this happens.

Does anyone have any solution?


Additional information.

When I use Xcode to RUN the app and the app received push notification in Background, that function is called in Background.

When I tap the app and and the app received push notification in Background, that function is called in Active but not called in Backgroiund.

Why?

Thank you for your response.


I did register like below;


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        let center = UNUserNotificationCenter.current()
        center.delegate = self 
        center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            if granted {
                DispatchQueue.main.async {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            } else {
                print(error?.localizedDescription as Any)
            }
        }
}


Do you see any problem?

Thank you for your response.


I will review the site you described.


> Note: you don't return true from didFinishLaunching,

I return "true" from didFinishLaunching. Do you mean I should return "false"?


> probably because you did not copy the complete func ?

What function(s) do I miss in didFinishLaunching?


I appreciate your kind advice.

No, I just meant I did not see return true in the func code. true is OK.


What I note in Apple code is that they register in all cases

    // Register with APNs
    UIApplication.shared.registerForRemoteNotifications()

Oh, OK.

Yes, I rerurn "true".


So, what do you think the reason that push notification does not call that function in Backgroiund?

It works fine if I use Xcode to RUN the app, but not work if I tap the app icon on my iPhone display and do the same operation.


Additional information.

The banner of push notification is correctly displayed on the screen, but "didReceiveRemoteNotification userInfo:" is not called. That is the problem. Only in Background. It works fine in Foreground and when removed from background.

What happens if you remove the dispatch (yes, I know they advise to use in in SO…)

i have a very simular problem. my Apps are running fine when i start them from Xcode on a device, running the App from Testflight always causes a crash on the very first screen and the app stops running.


In Xcode 11.2.1 my Capability Menue doesnt show "Background Tasks".

I tried, but still same symptom.


Thank you for your advice, anyway.

I am using Xcode 10.3.

Hello

I got same problem, can you let me know how did you resolve the issue?

Not sure if this relates. but I found out that when you run from Xcode (connected to Mac), it prevents app to go to Suspend. But without Xcode, the app can go to Suspend.

Push notification in Background works fine when App runs by Xcode, but does not work when App runs by itself.
 
 
Q