Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing silent push notifications in Xcode11.4 beta

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
 }
}
like image 670
Nikolai Prokofev Avatar asked Feb 12 '20 16:02

Nikolai Prokofev


2 Answers

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.

like image 174
Jeff Avatar answered Sep 30 '22 14:09

Jeff


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

Enable Background Fetch

like image 34
infiniteLoop Avatar answered Sep 30 '22 12:09

infiniteLoop