我正在使用 FCM(firebase 云消息传递)向 IOS 应用发送自定义"数据通知.据我了解,当您希望 FCM 代表您的应用处理显示通知时,我们会使用通知消息.当您只想在您的应用程序中处理消息时,我们会使用数据消息.这是设计使然.
I am using FCM (firebase cloud messaging) to send "custom" data notifications to an IOS app. From what I understand, we use notification messages when you want FCM to handle displaying a notification on your app's behalf. And we use data messages when you just want to process the messages only in your app. It's by design.
我面临的问题是 Device/InstandID 令牌对于已安装的应用程序是唯一的,而不是登录应用程序的用户.所以为了解决这个问题,我在数据中发送预期的用户标签,使其成为数据消息.由于应用程序处理数据通知,所以 didReceiveRemoteNotification() 回调仅在应用程序打开时触发,通知仅在发送时显示,而不是立即显示.
Problem I am facing is that that Device/InstandID token is unique to installed app and not the user logged in the app. So to solve this, I am sending intended user tag in the data so it becomes a data message. Since the app handles the data notifications, the didReceiveRemoteNotification() callback is only fired when the app is opened and the notification is only shown than and not instantly when it's sent.
我的问题是,我是否可以发送自定义数据通知消息并使其在应用程序关闭时立即显示.
My question is that can I send a custom data notification message and make it appear instantly even when the app is closed.
这是我发送给 FCM 的负载:
This is the payload I am sending to FCM:
{
registeration_ids : [<id_1>, <id_2>],
data : {
title : message_title,
body : message_body,
intended_user : message_user
}
}
在 android 中 FirebaseMessagingService.onMessageReceived()
被调用,即使应用程序在后台,但在 ios didReceiveRemoteNotification()
仅在应用程序启动时调用,所以没有如果您发送数据消息,则会出现背景消息.
In android FirebaseMessagingService.onMessageReceived()
is invoked even if the app is in the background but in ios didReceiveRemoteNotification()
is only invoked when the application is launched so no background messages will appear if you send a data message.
据我目前了解,在ios端没有办法正确解决这个问题.它在 android 端完美运行,因为应用程序在所有状态(前景、背景和关闭)下都被唤醒.
From what I understand so far, there is no way of solving this issue properly on the ios side. It works perfectly on the android side because the application is awoken in all states (foreground, background & closed).
您可以发送两种消息:
由操作系统直接显示和处理的通知消息.
A notification-message which is displayed and handled by the operating system directly.
由应用程序处理的数据消息.
如果您添加自定义标签,它现在会变成数据消息并且必须由应用程序处理.您可以在 data-message 中添加 content_available
标记,让应用程序知道该消息,但问题是只有在应用程序处于前台时,数据消息才会传递给 ios 中的应用程序(打开)或在后台(最小化).如果用户强制关闭"应用程序(即使启用了后台通知),数据消息将不会传递给应用程序.
If you add a custom tag, it now becomes a data-message and would have to be handled by the application. You can add a content_available
tag in data-message to let the application know about the message but the problem is that the data-message is only delivered to the application in ios if the application is in the foregrounded (opened) or in the background (minimised). The data-message will not be delivered to the application if the user has "force-closed" the application (even with the background notifications enabled).
解决方案是在服务器端处理目标用户,并通过维护设备令牌与用户的一对一关系来解决多用户对一个设备令牌的问题.
Solution is to handle the intended user on the server side and solve the multiple-users to one-device-token problem by maintaining the a one to one device-token to user relationship.
这篇关于带有 FCM 的 IOS 数据通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!