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
}
}