如何使用 Firebase 获取通知响应数据?
Posted
技术标签:
【中文标题】如何使用 Firebase 获取通知响应数据?【英文标题】:How to get notification response data using Firebase? 【发布时间】:2018-07-31 11:40:41 【问题描述】:我已经实现了 FCM 通知来发送通知。 当我从 firebase 控制台发送通知时,我会收到通知。但是当我从服务器发送通知时,我得到了成功但通知没有出现..
其次,如何获取通知响应数据?
我的didReceiveRemoteNotification
功能也不起作用。这是我的代码:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
print("Recived: \(userInfo)")
print("success")
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
print("Recived: \(userInfo)")
print("success")
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
print("Recived: \(userInfo)")
print("success")
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
self.application(application, didReceiveRemoteNotification: userInfo) (UIBackgroundFetchResult) in
print("Recived: \(userInfo)")
print("success")
print("Recived: \(userInfo)")
print("success")
let state: UIApplicationState = application.applicationState
// user tapped notification while app was in background
if state == .inactive || state == .background
print("Inactive")
else
print("Active")
【问题讨论】:
如何从服务器发送通知负载?你能把那个payload贴在这里吗... 当您从服务器发送通知时,您使用的是 FCM 令牌还是设备令牌? 我在从服务器发送通知时使用 FCM 令牌 我正在使用这个通知负载。 "aps" : "alert" : "带有自定义负载的通知!", "badge" : 1, "content-available" : 1 , "data" : "title" : "Game Request", "body" : "Bob 想玩扑克", "action-loc-key" : "PLAY" 我在使用 Firebase Admin Java SDK 时遇到了这个问题,通知到达 android 设备。但不适用于 ios 设备。并且使用 Firebase 控制台也可以,使用 Legacy Api 也可以。 【参考方案1】:我发现下面的消息最适合我在 Android 和 iOS 上使用(我使用 firebase 函数来发送它,但这不重要)。请注意,要使其正常工作,您需要使用 send 方法而不是已弃用的方法之一,例如:admin.messaging().send(message)
要发送给send
的消息:
const message =
token: addresseePushToken,
/*
Apple Push Notification Service
https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns
*/
apns:
headers:
'apns-priority': 10, // defaults to 10 (send immediately), can be set to 5 (consider device power)
'apns-collapse-id': chatId // group multiple notifications into a single notification (must not exceed 64 bytes)
,
payload:
aps:
alert:
title: title,
body: doc.message,
,
badge: unseenCount+1,
,
/*
Android specifics
https://firebase.google.com/docs/cloud-messaging/admin/send-messages#android_specific_fields
*/
android:
priority: 'high', // either 'normal' or 'high' - high send immediately & waking sleeping device
notification: // notification object creates status bar notification if app in background
tag: chatId, // key for grouping notifications (Android only)
title: title,
body: doc.message,
color: '#ffffff', // notification's icon color
icon: thumbUrl, // if not specified FCM displays the launcher icon as defined in app manifest
,
// data object is available to the app both when received in foreground and background
data:
type: 'chat',
senderId: senderId,
title: title,
body: doc.message,
,
【讨论】:
【参考方案2】:我解决了一个与此非常相似的问题,将其添加到 Firebase 通知中:
"apns" :
"payload" :
"aps" :
"content-available" : 1
【讨论】:
以上是关于如何使用 Firebase 获取通知响应数据?的主要内容,如果未能解决你的问题,请参考以下文章
Flutter:如何从 Firebase 通知负载中获取 Json 数据