FCM(Firebase 云消息传递)发送到多个设备

FCM (Firebase Cloud Messaging) Send to multiple devices(FCM(Firebase 云消息传递)发送到多个设备)

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

问题描述

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

public string PushFCMNotification(string deviceId, string message){字符串 SERVER_API_KEY = "xxxxxxxxxxxxxxxxxxxxxxx";var SENDER_ID = "xxxxxxxxx";变量值 = 消息;WebRequest tRequest;tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");tRequest.Method = "发布";tRequest.ContentType = "应用程序/json";tRequest.Headers.Add(string.Format("授权:key={0}", SERVER_API_KEY));tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));var 数据 = 新{至 = 设备 ID,通知=新{body = "这是消息",title = "这是标题",图标 = 我的图标"}};var serializer = new JavaScriptSerializer();var json = serializer.Serialize(data);Byte[] 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);字符串 sResponseFromServer = tReader.ReadToEnd();tReader.Close();数据流.关闭();tResponse.Close();返回 sResponseFromServer;}

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

你能帮忙吗

解决方案

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


只需使用 registration_ids 参数而不是有效负载中的 to.同样根据您的用例,您可以使用 主题消息传递或设备组消息传递.

<块引用>

主题消息传递

Firebase 云消息传递 (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 云消息传递)发送到多个设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:FCM(Firebase 云消息传递)发送到多个设备

基础教程推荐