我有一个大问题,我在我的 iOS 项目上设置了(非常困难)推送通知。我决定在“AppDelegate”的“didReceiveRemoteNotification”方法中接收通知数据,然后以编程方式创建它(以执行强制性个人处理)。一切正常,只有当我在没有 Xcode 帮助的情况下启动我的应用程序时,我不再收到通知,通知创建代码没有执行。我不知道该怎么办......
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping
(UIBackgroundFetchResult) -> Void) {
let body = userInfo["body"]
print(userInfo)
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
content.body = body! as! String
let trigger = UNTimeIntervalNotificationTrigger(timeInterval:
1, repeats: false)
let request = UNNotificationRequest(identifier: "idt", content:
content, trigger: trigger)
UNUserNotificationCenter.current().add(request,
withCompletionHandler: nil)
} else {
// Fallback on earlier versions
}
completionHandler(UIBackgroundFetchResult.newData)
}
非常感谢
Best Answer-推荐答案 strong>
当您从通知启动应用时,您需要检查 application:didFinishLaunchingWithOptions: 中的 launchOptions 以查看 UIApplicationLaunchOptionsRemoteNotificationKey 是否存在。
根据Documentation :
The value of this key is an NSDictionary containing the payload of the remote notification. See the description of application:didReceiveRemoteNotification: for further information about handling remote notifications.
This key is also used to access the same value in the userInfo dictionary of the notification named UIApplicationDidFinishLaunchingNotification.
一旦您确定启动包含通知,请使用从 launchOptions 获得的 通知负载 调用通知处理程序方法。
What you have now ,即 application(_:didReceiveRemoteNotification 仅在应用已经运行时触发:
If the app is running, the app calls this method to process incoming remote notifications. The userInfo dictionary contains the aps key whose value is another dictionary with the remaining notification
关于ios - 推送通知仅在使用 Xcode 启动应用程序时有效,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/46957318/
|