带有FCM的IOS数据通知 [英] IOS data notifications with FCM

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

问题描述

我正在使用FCM(firebase云消息)将自定义数据通知发送到IOS应用程序。据我所知,当您希望FCM代表应用程序显示通知时,我们会使用通知消息。而我们只在您的应用程序中只处理消息时使用数据消息。这是设计。



我正面临的问题是,Device / InstandID令牌对于已安装的应用程序是唯一的,而不是登录应用程序的用户。所以为了解决这个问题,我在数据中发送了预期的用户标签,所以它变成了一个数据信息。由于应用程序处理数据通知,因此只有在应用程序打开时才会触发didReceiveRemoteNotification()回调,并且通知只会在发送时显示而不是立即显示。



我的问题是,我可以发送一个自定义的数据通知消息,并使其即使应用程序关闭即时显示。



这是我发送给FCM的有效载荷:

  { 
registeration_ids:[< id_1>,< id_2>],
data:{
title:message_title,
body:message_body,
intended_user:message_user




$在android FirebaseMessagingService.onMessageReceived()即使应用程序在后台,但在ios didReceiveRemoteNotification()中,仅在应用程序启动时调用,所以不会显示任何后台消息如果您发送数据消息。

解决方案

我注意到,通知优先级为设置为高。

  {notification:{
body:本周版本现在可用。,
标题:葡萄牙对丹麦,
text:5比1,
content_available:1
},
数据: {
volume:3.21.15,
contents:http://www.news-magazine.com/world-week/21659772
},
转换为:fqUk65A1kTE:APA91bG5 ...,//或设置主题如/ topics / test
priority:high
}

对于iOS客户端应用程序,正常优先级和高优先级类似于APN的优先级5和10.



在iOS文档中,可能会发送默认优先级通知。


apns-priority:优先级的通知。指定以下值之一:

10 - 立即发送推送消息。具有此优先级
的通知必须在目标设备上触发警报,声音或徽章。对于仅包含
内容可用密钥的推送通知,使用此优先级是
错误。

5-发送推送消息时,
考虑了设备的功耗考虑事项。具有此优先级的通知
可能被分组并以突发形式递送。他们是
扼杀,并在某些情况下不交付。如果您省略
标题,则APNs服务器将优先级设置为10。上面的通知是通过FCM API发送的,它是在应用程序被杀的时候收到的。
当我点击设备上的通知后,这是应用程序启动后在设备日志中显示的内容:

  .. .didReceiveRemoteNotification:[gcm.message_id:0:1468481012881485%e1d60a46e1d60a46,volume:3.21.15,aps:{
alert = {
body =This week \U2019s edition is now available。;
title =葡萄牙对丹麦;
};
},内容:http://www.news-magazine.com/world-week/21659772] [;

请注意,数据部分也包含在收到的消息中。



更新2:

单个设备上的多个用户或多个设备上的一个用户。

在服务器端,您必须确保一个唯一的fcm_id只能分配给一个用户。这意味着一个设备被分配给一个用户,所以每个设备只有一个用户会被通知。

另外,一个用户可以有多个fcm_ids,这意味着用户可以有更多的设备并登录。

当user_2登录到user_1的同一设备时,fcm_id必须从user_1分离,并且连接到user_2。在这种方式下,只有当前登录的用户才会收到消息。


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.

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.

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
    }
}

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.

解决方案

I noticed that notification will arrive when app is force-closed in case that notification priority is set to high.

{  "notification": {
    "body" : "This week’s edition is now available.",
    "title": "Portugal vs. Denmark",
    "text": "5 to 1",
    "content_available": 1
  },
  "data" : {
    "volume" : "3.21.15",
    "contents" : "http://www.news-magazine.com/world-week/21659772"
  },
  "to" : "fqUk65A1kTE:APA91bG5...", // or set topic like "/topics/test"
  "priority" : "high"
}

For iOS client apps, normal and high priority are analogous to APNs priority levels 5 and 10.

From the iOS documentation it is possible that notification with default priority are not delivered.

apns-priority: The priority of the notification. Specify one of the following values:

10–Send the push message immediately. Notifications with this priority must trigger an alert, sound, or badge on the target device. It is an error to use this priority for a push notification that contains only the content-available key.

5—Send the push message at a time that takes into account power considerations for the device. Notifications with this priority might be grouped and delivered in bursts. They are throttled, and in some cases are not delivered. If you omit this header, the APNs server sets the priority to 10.

Update 1:

The above notification is sent through FCM api and it was received while app was killed. After I tap the notification on device this is what appears in device log after the app has started:

...didReceiveRemoteNotification: [gcm.message_id: 0:1468481012881485%e1d60a46e1d60a46, volume: 3.21.15, aps: {
        alert =     {
            body = "This week\U2019s edition is now available.";
            title = "Portugal vs. Denmark";
        };
    }, contents: http://www.news-magazine.com/world-week/21659772][;

Notice that data part is also contained in received message.

Update 2:

Multiple users on single device or one user with many devices.

On your server side you must assure that one unique fcm_id can be assigned only to one user. This means that one device is assigned to one user, so only one user will be notified per device.

Also, one user can have multiple fcm_ids, so it means user can have more devices and be signed in.

When the user_2 log in to same device of user_1, fcm_id must be detached from the user_1 and attached to user_2. Within this way, only currently logged in user will receive the message.

这篇关于带有FCM的IOS数据通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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