我希望在Xcode 11.4模拟器中测试通知,除了静默通知之外,一切都运行良好。没有触发didReceiveRemoteNotification。
我所做的:
启用后台模式的推送通知/功能中的远程通知
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
}在didReceiveRemoteNotification中,我只是在UserDefaults中设置值,以便以后使用它。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print(userInfo)
UserDefaults.standard.set("Hello world", forKey: "hello")
completionHandler(.newData)
}然后在sceneDidBecomeActive中如何从UserDefaults读取这个值
func sceneDidBecomeActive(_ scene: UIScene) {
print(UserDefaults.standard.value(forKey: "hello")) // always nil
}这是我的.apns json
{
"Simulator Target Bundle": "xxx",
"aps" : {
"content-available" : 1
}
}发布于 2020-04-01 21:22:07
看来,可能是由于一个错误(我相信已经报告过),apns文件被调用,而不是didReceiveRemoteNotification被调用。
发布于 2020-04-08 06:22:08
Jeff的解决方案对我有效,此外,我还必须在app中添加“后台获取”模式

https://stackoverflow.com/questions/60192628
复制相似问题