diff --git a/CHANGELOG.md b/CHANGELOG.md index a2358184..20a955b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Unreleased -- +- [added] Implemented the `SendAllAsync()` and `SendMulticastAsync()` APIs in + the `FirebaseMessaging` class. # v1.2.1 diff --git a/FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseMessagingSnippets.cs b/FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseMessagingSnippets.cs index 8817d539..436de0ae 100644 --- a/FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseMessagingSnippets.cs +++ b/FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseMessagingSnippets.cs @@ -14,6 +14,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using FirebaseAdmin.Messaging; @@ -117,6 +118,107 @@ internal static async Task SendDryRunAsync() // [END send_dry_run] } + internal static async Task SendAllAsync() + { + var registrationToken = "YOUR_REGISTRATION_TOKEN"; + // [START send_all] + // Create a list containing up to 100 messages. + var messages = new List() + { + new Message() + { + Notification = new Notification() + { + Title = "Price drop", + Body = "5% off all electronics", + }, + Token = registrationToken, + }, + new Message() + { + Notification = new Notification() + { + Title = "Price drop", + Body = "2% off all books", + }, + Topic = "readers-club", + }, + }; + + var response = await FirebaseMessaging.DefaultInstance.SendAllAsync(messages); + // See the BatchResponse reference documentation + // for the contents of response. + Console.WriteLine($"{response.SuccessCount} messages were sent successfully"); + // [END send_all] + } + + internal static async Task SendMulticastAsync() + { + // [START send_multicast] + // Create a list containing up to 100 registration tokens. + // These registration tokens come from the client FCM SDKs. + var registrationTokens = new List() + { + "YOUR_REGISTRATION_TOKEN_1", + // ... + "YOUR_REGISTRATION_TOKEN_n", + }; + var message = new MulticastMessage() + { + Tokens = registrationTokens, + Data = new Dictionary() + { + { "score", "850" }, + { "time", "2:45" }, + }, + }; + + var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message); + // See the BatchResponse reference documentation + // for the contents of response. + Console.WriteLine($"{response.SuccessCount} messages were sent successfully"); + // [END send_multicast] + } + + internal static async Task SendMulticastAndHandleErrorsAsync() + { + // [START send_multicast_error] + // These registration tokens come from the client FCM SDKs. + var registrationTokens = new List() + { + "YOUR_REGISTRATION_TOKEN_1", + // ... + "YOUR_REGISTRATION_TOKEN_n", + }; + var message = new MulticastMessage() + { + Tokens = registrationTokens, + Data = new Dictionary() + { + { "score", "850" }, + { "time", "2:45" }, + }, + }; + + var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message); + if (response.FailureCount > 0) + { + var failedTokens = new List(); + for (var i = 0; i < response.Responses.Count; i++) + { + if (!response.Responses[i].IsSuccess) + { + // The order of responses corresponds to the order of the registration tokens. + failedTokens.Add(registrationTokens[i]); + } + } + + Console.WriteLine($"List of tokens that caused failures: {failedTokens}"); + } + + // [END send_multicast_error] + } + internal static Message CreateAndroidMessage() { // [START android_message] diff --git a/FirebaseAdmin/FirebaseAdmin/Messaging/SendResponse.cs b/FirebaseAdmin/FirebaseAdmin/Messaging/SendResponse.cs index d94806e8..6b4e3471 100644 --- a/FirebaseAdmin/FirebaseAdmin/Messaging/SendResponse.cs +++ b/FirebaseAdmin/FirebaseAdmin/Messaging/SendResponse.cs @@ -44,9 +44,9 @@ private SendResponse(FirebaseException exception) /// /// Gets a value indicating whether the send operation was successful or not. When this property - /// is , is guaranteed to return a - /// non- value. When this property is , - /// is guaranteed to return a non- value. + /// is true, is guaranteed to return a + /// non-null value. When this property is false, + /// is guaranteed to return a non-null value. /// public bool IsSuccess => !string.IsNullOrEmpty(this.MessageId);