Firebase Cloud Messaging是否有回调指示收到消息? [英] is there a callback from Firebase Cloud Messaging to indicate that a message is received?
问题描述
在此处中的文档中,有两种方法可以收到推送通知.但正如评论所言,只有当用户在后台运行时点击通知时,它才会被触发.
from the documentation in here, there are two methods to receive a push notification. but as the comment say, it is only fired when the user tap the notification when app is in the background.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
我想要的是,每当有推送通知到来时,无论用户在后台还是在前台点击推送通知,我都想执行一些操作.所以每当收到消息时我都需要回调
what I want is, whenever a push notification comes, then I want to do some action no matter if the user tap the push notification or not, in the background or in the foreground. so I need a callback whenever a message is received
在Android中,我可以在 onMessageReceived
回调
in Android I can do it in onMessageReceived
callback
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
}
在iOS中是否有类似的回调?我找不到
is there any equivalent callback like that in iOS ? I can't find it
推荐答案
您可以通过在通知有效负载中指定 content_available
来向iOS应用发送静默通知.设置为true时,通知将在后台唤醒您的应用,您可以在 application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
委托方法中处理通知.
You can send a silent notification to iOS app by specifying content_available
in the notification payload. When it is set to true, the notification will wake your app in the background and you can handle the notification in application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
delegate method.
您可以在此处中找到更多信息.
You can find more about here.
"to" : "[token]",
"content_available": true,
"priority": "high",
"data" : {
"key1" : "abc",
"key2" : abc
}
这篇关于Firebase Cloud Messaging是否有回调指示收到消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!