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

标签 android asp.net firebase-cloud-messaging

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

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

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

你能帮忙吗

最佳答案

更新:对于 v1,似乎不再支持 registration_ids。强烈建议改用主题。只有documentation 中显示的参数支持 v1。


只需使用 registration_ids参数而不是有效负载中的 to。根据您的用例,您可以使用 Topic MessagingDevice 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.


更多详情,您可以查看Sending to Multiple Devices in FCM文档。

关于android - FCM(Firebase 云消息传递)发送到多个设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39547277/

相关文章:

android - 为什么找不到adb?

java - Google 登录会删除现有用户数据

java - 如何使用 FCM 管理聊天应用程序的推送通知

ios - UNNotificationServiceExtension 不适用于 Firebase Cloud Functions

java.lang.IllegalArgumentException : Object would be serialized to `null` : Android 异常

java - Dagger:POJO 中的字段注入(inject)

javascript - 用于提供网站反馈的按钮的固定位置 CSS

c# - Android -- 如何通过应用程序访问 ASP.NET 数据库中的数据?

asp.net - 在 ASP.net 页面中嵌入 SVG

cordova - 使用 Ionic 应用程序进行 Firebase 云消息传递