FCM(Firebase Cloud Messaging)发送到多个设备 [英] FCM (Firebase Cloud Messaging) Send to multiple devices

查看:50
本文介绍了FCM(Firebase Cloud Messaging)发送到多个设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行此代码以使用 FCM 库将通知推送到移动设备

public string PushFCMNotification(string deviceId, string message){string SERVER_API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxx";var SENDER_ID = "xxxxxxxxx";var 值 = 消息;WebRequest tRequest;tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");tRequest.Method = "post";tRequest.ContentType = "应用程序/json";tRequest.Headers.Add(string.Format("Authorization: key={0}", SERVER_API_KEY));tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));var 数据 = 新{to = deviceId,通知 = 新{body = "这是消息",title = "这是标题",图标 = "myicon"}};var serializer = new JavaScriptSerializer();var json = serializer.Serialize(data);字节[] byteArray = Encoding.UTF8.GetBytes(json);tRequest.ContentLength = byteArray.Length;流数据流 = tRequest.GetRequestStream();dataStream.Write(byteArray, 0, byteArray.Length);数据流.关闭();WebResponse tResponse = tRequest.GetResponse();数据流 = tResponse.GetResponseStream();StreamReader tReader = new StreamReader(dataStream);String sResponseFromServer = tReader.ReadToEnd();tReader.Close();数据流.关闭();tResponse.Close();返回 sResponseFromServer;}

现在,如何向多设备发送消息,假设字符串 deviceId 参数替换为 List devicesIDs.

你能帮忙吗

解决方案

更新:对于v1,似乎不再支持registration_ids.强烈建议改用主题.v1 仅支持文档中显示的参数.


只需使用 registration_ids 参数而不是您的有效负载中的 to.根据您的用例,您可以使用 Topic Messaging设备组消息.

<块引用>

主题消息

Firebase Cloud Messaging (FCM) 主题消息传递让您可以向选择加入特定主题的多台设备发送消息.基于发布/订阅模型,主题消息支持每个应用程序的无限订阅.您可以根据需要撰写主题消息,Firebase 会处理消息路由并将消息可靠地传送到正确的设备.

例如,本地天气预报应用的用户可以选择接收恶劣天气警报"主题并接收威胁特定区域的风暴通知.体育应用程序的用户可以订阅他们最喜欢的球队的实时比赛比分的自动更新.开发者可以选择任意匹配正则表达式的主题名称:"/topics/[a-zA-Z0-9-_.~%]+".


<块引用>

设备组消息

通过设备组消息传递,应用服务器可以向在属于一个组的设备上运行的多个应用实例发送一条消息.通常,组"指属于单个用户的一组不同设备.组中的所有设备共享一个公共通知密钥,这是 FCM 用于向组中的所有设备发送消息的令牌.

设备组消息使组中的每个应用实例都可以反映最新的消息状态.除了将消息向下游发送到通知键之外,您还可以使设备向设备组发送上游消息.您可以通过 XMPP 或 HTTP 连接服务器使用设备组消息传递.发送到iOS设备时数据负载限制为2KB,其他平台为4KB.

notification_key 允许的最大成员数为 20.


有关更多详细信息,您可以查看发送到多个设备FCM 文档.

I execute this code to push notifications to mobile device using FCM library

public string PushFCMNotification(string deviceId, string message) 
    {
        string SERVER_API_KEY = "xxxxxxxxxxxxxxxxxxxxxxx";
        var SENDER_ID = "xxxxxxxxx";
        var value = message;
        WebRequest tRequest;
        tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = "application/json";
        tRequest.Headers.Add(string.Format("Authorization: key={0}", SERVER_API_KEY));

        tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

        var data = new
        {
            to = deviceId,
            notification = new
            {
                body = "This is the message",
                title = "This is the title",
                icon = "myicon"
            }
        };

        var serializer = new JavaScriptSerializer();
        var json = serializer.Serialize(data);

        Byte[] byteArray = Encoding.UTF8.GetBytes(json);

        tRequest.ContentLength = byteArray.Length;

        Stream dataStream = tRequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        WebResponse tResponse = tRequest.GetResponse();

        dataStream = tResponse.GetResponseStream();

        StreamReader tReader = new StreamReader(dataStream);

        String sResponseFromServer = tReader.ReadToEnd();


        tReader.Close();
        dataStream.Close();
        tResponse.Close();
        return sResponseFromServer;
    }

now, how to send message to multi device, assume that string deviceId parameter replaced with List devicesIDs.

can you help

解决方案

Update: For v1, it seems that registration_ids is no longer supported. It is strongly suggested that topics be used instead. Only the parameters shown in the documentation are supported for v1.


Simply use the registration_ids parameter instead of to in your payload. Depending also on your use case, you may use either Topic Messaging or Device Group Messaging.

Topic Messaging

Firebase Cloud Messaging (FCM) topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. Based on the publish/subscribe model, topic messaging supports unlimited subscriptions for each app. You compose topic messages as needed, and Firebase handles message routing and delivering the message reliably to the right devices.

For example, users of a local weather forecasting app could opt in to a "severe weather alerts" topic and receive notifications of storms threatening specified areas. Users of a sports app could subscribe to automatic updates in live game scores for their favorite teams. Developers can choose any topic name that matches the regular expression: "/topics/[a-zA-Z0-9-_.~%]+".


Device Group Messaging

With device group messaging, app servers can send a single message to multiple instances of an app running on devices belonging to a group. Typically, "group" refers a set of different devices that belong to a single user. All devices in a group share a common notification key, which is the token that FCM uses to fan out messages to all devices in the group.

Device group messaging makes it possible for every app instance in a group to reflect the latest messaging state. In addition to sending messages downstream to a notification key, you can enable devices to send upstream messages to a device group. You can use device group messaging with either the XMPP or HTTP connection server. The limit on data payload is 2KB when sending to iOS devices, and 4KB for other platforms.

The maximum number of members allowed for a notification_key is 20.


For more details, you can check out the Sending to Multiple Devices in FCM docs.

这篇关于FCM(Firebase Cloud Messaging)发送到多个设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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