使用 FCM 的 IOS 数据通知 [英] IOS data notifications with FCM

查看:17
本文介绍了使用 FCM 的 IOS 数据通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 标签,让应用程序知道该消息,但问题是如果应用程序处于前台(打开)或在后台(最小化).如果用户强制关闭"了应用程序(即使启用了后台通知),数据消息将不会传送到应用程序.

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 数据通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆