【发布时间】:2020-05-28 06:20:11
【问题描述】:
我想在 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
}
}
【问题讨论】:
标签: swift xcode push-notification apple-push-notifications