Dear fellow developers,
I have an issue where my didReceiveRemoteNotification
method in the AppDelegate is never called. Here's what I did:
-
I am developing a SwiftUI application for iOS 15 and 16 and am currently running tests in an iPhone 14 Pro Simulator. My Xcode version is 14.2 and the simulator is running iOS 16.2.
-
I am of course talking about the AppDelegate's
application:didReceiveRemoteNotification:fetchCompletionHandler:
method, not the deprecated one. -
I embedded the AppDelegate in the following way:
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
...
}
-
The integration of my AppDelegate does work in general, meaning the
didFinishLaunchingWithOptions
is called. -
I added the background modes capability to my app, including both "Remote notifications" and "Background processing".
With this configuration, I would expect the AppDelegate's method to be called. I tried sending different push notifications with different payloads and headers while having the app both in the background and foreground but without success.
Just to rule out mistakes on my side that are obvious to someone else, this is how I embedded the method into my AppDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
...
func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
print("Method called!")
completionHandler(.noData)
}
}
And here are a few payloads and headers that I tested. I tried all of the following payloads with the following headers in any combination.
apns-push-type=alert
orapns-push-type=background
apns-priority=5
orapns-priority=10
Example 1: Visible notification
{
"aps": {
"alert": {
"body": "Test"
},
"badge": 1,
"content-available": 1
},
"test": "data"
}
Example 2: Silent notification without badge update
{
"aps": {
"content-available": 1
},
"test": "data"
}
Example 3: Silent notification with empty alert
block and badge update, based on ideas from this thread
{
"aps": {
"alert": {},
"badge": 0,
"content-available": 1
},
"test": "data"
}
As I said, no matter whether my app is in the foreground or background, the method is never called. At the same time, the userNotificationCenter(_:willPresent:withCompletionHandler:)
method of the UNUserNotificationCenterDelegate
is called when the app is in the foreground. At this point, I am running out of ideas what I could possibly be doing wrong, so I hope someone else has an idea what else to try.