bytefish / FcmSharp

Firebase Cloud Messaging (FCM) with .NET
MIT License
6 stars 7 forks source link

How can I send message to multiple tokens? #59

Closed ianmark89 closed 5 years ago

bytefish commented 5 years ago

You have create a message for each of your registration tokens. The Firebase Messaging v1 API does not have something like a specific Multicast message. Creating a message for each token is what Google does in the Firebase SDK for Java:

ianmark89 commented 5 years ago

So I have create new method in FCM client for sending list of messages?

bytefish commented 5 years ago

Let me see, if I can add an extension method to the library. Give me a moment. 👍

bytefish commented 5 years ago

Oh look how wrong I was!

image

There is actually a Batch Endpoint!

bytefish commented 5 years ago

I will implement it, but it may take a day. 👍 Thanks for raising the issue. I didn't even know, that Request Batching is supported. That makes a great feature. 👍 What do you think about releasing it with 3.0? Then I can correct some questionable design decisions in the API.

bytefish commented 5 years ago

I think the simplest approach is to see, what they did in the TypeScript implementation:

I had a look into the Google .NET Client Implementation, there is actually a BatchRequest class. But it's so overdesigned, I don't really want to use it. I think for this Firebase API implementation it is better to keep it simple and duplicate it.

That makes it easier for me to understand it and better to maintain!

bytefish commented 5 years ago

Added the SendBatch Method in 3.0.0 to send Messages to Multiple Devices in one request. Thanks for the nice feature. 👍

bytefish commented 5 years ago

I have added a method SendMulticastMessage to the API. Sending a Multicast message is now as easy as writing:

var result = client.SendMulticastMessage(tokens, message)

Here is a full example:

// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;
using System.Threading;
using FcmSharp.Requests;
using FcmSharp.Settings;
using NUnit.Framework;

namespace FcmSharp.Test.Integration
{
    [TestFixture]
    class BatchMessageIntegrationTest
    {
        [Test]
        public void SendBatchMessages()
        {
            // This is a list of Tokens I want to send the Batch Message to:
            var tokens = File.ReadAllLines(@"D:\device_tokens.txt");

            // Read the Credentials from a File, which is not under Version Control:
            var settings = FileBasedFcmClientSettings.CreateFromFile(@"D:\serviceAccountKey.json");

            // Construct the Client:
            using (var client = new FcmClient(settings))
            {

                Message message = new Message
                {
                    Notification = new Notification
                    {
                        Title = "Notification Title",
                        Body = "Notification Body Text"
                    }
                };

                // Finally send the Message and wait for the Result:
                CancellationTokenSource cts = new CancellationTokenSource();

                // Send the Message and wait synchronously:
                var result = client.SendMulticastMessage(tokens, message, false, cts.Token).GetAwaiter().GetResult();

                // Print the Result to the Console:
                foreach (var response in result.Responses)
                {
                    Assert.IsNotNull(response.Name);
                }
            }
        }
    }
}