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
}
}
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.
Jeff's Solution worked for Me, In addition I had to add 'Background Fetch' mode to app
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With