gpt4 book ai didi

ios - 推送通知仅在使用 Xcode 启动应用程序时有效

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:51 26 4
gpt4 key购买 nike

我有一个大问题,我在我的 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)
}

非常感谢

最佳答案

当您从通知启动您的应用程序时,您需要检查 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com