Closed ianmark89 closed 5 years ago
So I have create new method in FCM client for sending list of messages?
Let me see, if I can add an extension method to the library. Give me a moment. 👍
Oh look how wrong I was!
There is actually a Batch Endpoint!
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.
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!
Added the SendBatch
Method in 3.0.0 to send Messages to Multiple Devices in one request. Thanks for the nice feature. 👍
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);
}
}
}
}
}
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: