1

Is there any way to broadcast an FCM notification--plus an image--to all devices, using C#?

Instead of sending to one specific device ID, I want to include an image and send to all devices over Firebase Notification service.

I used this code to send data to a single user device, but without an image:

public string SendNotificationInstaTips(string firebaseID, 
        string notTitle
        string notText, 
        string notContent)
    {

        try
        {
            string SERVER_API_KEY = "AIza..QXq5OQCaM";
            string SENDER_ID = "162..09";                
            string REGISTERATION_ID = firebaseID;

            WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = "application/json";

            var data = new
            {
                to = REGISTERATION_ID,

                data = new
                {
                    title = notTitle,
                    text = notText
                    content = notContent
                }
            };
            var serializer = new JavaScriptSerializer();
            var json = serializer.Serialize(data);
            Byte[] byteArray = Encoding.UTF8.GetBytes(json);
            tRequest.Headers.Add(string.Format("Authorization: key={0}", SERVER_API_KEY));
            tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
            tRequest.ContentLength = byteArray.Length;
            using (Stream dataStream = tRequest.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
                using (WebResponse tResponse = tRequest.GetResponse())
                {
                    using (Stream dataStreamResponse = tResponse.GetResponseStream())
                    {
                        using (StreamReader tReader = new StreamReader(dataStreamResponse))
                        {
                            String sResponseFromServer = tReader.ReadToEnd();
                            return sResponseFromServer;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
Salar Rastari
  • 2,280
  • 5
  • 22
  • 35

2 Answers2

0

I've been working on this with PHP but it should be similar in C#. Use "registration ids" instead of "to" in your code

var data = new
        {
            to = REGISTERATION_ID, //Change this line

            data = new
            {
                title = notTitle,
                text = notText
                content = notContent
            }
        };
var data = new
        {
            registration_ids = {"AIza..QXq5OQCaM","An2i..QXq5OQCaM", .....},

            data = new
            {
                title = notTitle,
                text = notText
                content = notContent
            }
        };

hope this helps

Exairie
  • 1
  • 1
0

This is not the usual use-case for Push Notifications. For FCM, it is strongly advised not to send over images, primarily because of the payload size limit - 2KB for notification, 4KB for data.

I suggest using Firebase Storage for storing the images, then downloading it in the device when needed, just sending the download URL in the push notification instead as a workaround.

For your question on how to send to multiple devices, see my answer here. What I would suggest for your use-case to use Topic Messaging.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281