Testing silent push notifications in Xcode11.4 beta
Asked Answered
J

2

9

I want to test notifications in Xcode 11.4 simulator and everything works well except silent notifications. didReceiveRemoteNotification is not triggered.

What I've done:

Enabled Push Notifications in Background Modes/Remote Notifications in Capabilities

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, _ in
        if granted {
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }
    return true
}

In didReceiveRemoteNotification I just set value in UserDefaults to use it later

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print(userInfo)
    UserDefaults.standard.set("Hello world", forKey: "hello")
    completionHandler(.newData)
}

And then in sceneDidBecomeActive what to read this value from UserDefaults

    func sceneDidBecomeActive(_ scene: UIScene) {
    print(UserDefaults.standard.value(forKey: "hello")) // always nil
}

This is my .apns json

{
"Simulator Target Bundle": "xxx",
 "aps" : {
    "content-available" : 1
 }
}
Jephthah answered 12/2, 2020 at 16:12 Comment(0)
S
18

It appears that, presumably due to a bug (that I believe has been reported), the apns file is resulting in performFetchWithCompletionHandler being called, rather than didReceiveRemoteNotification being called.

Stringed answered 1/4, 2020 at 21:22 Comment(1)
Hi, how do you handle the notification inside performFetchWithCompletionHandler as it hasn't the userInfo which contains the notification?Stockroom
M
6

Jeff's Solution worked for Me, In addition I had to add 'Background Fetch' mode to app

Enable Background Fetch

Mesics answered 8/4, 2020 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.