如何在swift(FCM)中接收数据通知
我需要在swift中接收数据通知,而不是使用通知消息。仅使用数据消息 这是我写给邮递员的Json如何在swift(FCM)中接收数据通知,swift,firebase,push-notification,firebase-cloud-messaging,Swift,Firebase,Push Notification,Firebase Cloud Messaging,我需要在swift中接收数据通知,而不是使用通知消息。仅使用数据消息 这是我写给邮递员的Json { "to": "e32uafd....", "data": { "urlImage": "https://upload.wikimedia.org/wikipedia/commons/8/80/image.jpg", "title": "title", "body": "****body****.", "mutable_c
{
"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:UIApplication,DidReceiveMemoteNotification userInfo:[AnyHashable:Any],fetchCompletionHandler completionHandler:@escaping(UIBackgroundFetchResult)->Void)获取通知数据
但在执行此操作之前,您必须使您的ViewController成为注册通知时或在didFinishLaunchWithOptions(:)
方法中的UNUserNotificationCenter
的代表
步骤1
使您的AppDelegate成为UNNotificationCenter的代理
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.
}
注册通知时,您必须将您的应用程序代理指定为
UNUserNotificationCenter
的代理。我编辑了我的答案只是为了澄清。你确定你的应用程序收到推送通知吗?不,我没有收到通知。但是如果我添加“通知”:{“标题”:“标题”,“正文”:“****正文****”,“可变内容”:true},我会收到通知,但这就是问题所在,我不需要使用通知消息。仅使用数据消息