gpt4 book ai didi

swift - 如何在swift(FCM)中接收数据通知

转载 作者:行者123 更新时间:2023-11-30 10:32:55 25 4
gpt4 key购买 nike

我需要快速接收数据通知,而不是使用通知消息。仅使用数据消息。

这是我的 postman Json

{

"to": "e32uafd....",

"data": {
"urlImage": "https://upload.wikimedia.org/wikipedia/commons/8/80/image.jpg",
"title": "title",
"body": "****body****.",
"mutable_content": true
}
}

这是我的 swift 代码

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void)
{
switch UIApplication.shared.applicationState
{
case .active:
completionHandler( [.alert,.sound])
break
default:
completionHandler( [.alert,.sound])
break
}
}
}
}

最佳答案

您可以使用 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) 获取通知数据但在此之前,您必须使您的 ViewController 成为 UNUserNotificationCenter 的委托(delegate)注册通知时或在 didFinishLaunchWithOptions(:) 中方法。

第 1 步让你的AppDelegate成为UNNotificationCenter的委托(delegate)

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// Assumming you have imported NotificationCenter
UNUserNotificationCenter.current().delegate = self
//......

return true
}


第 2 步获取您的信息

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

print(userInfo) // Your notification info
// Note that you have to cast/parse it depending on which data you want to handle.


}

关于swift - 如何在swift(FCM)中接收数据通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58740142/

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