Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

-
- [added] Implemented the `SendAllAsync()` and `SendMulticastAsync()` APIs in
the `FirebaseMessaging` class.

# v1.2.1

Expand Down
102 changes: 102 additions & 0 deletions FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseMessagingSnippets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FirebaseAdmin.Messaging;

Expand Down Expand Up @@ -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<Message>()
{
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<string>()
{
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n",
};
var message = new MulticastMessage()
{
Tokens = registrationTokens,
Data = new Dictionary<string, string>()
{
{ "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<string>()
{
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n",
};
var message = new MulticastMessage()
{
Tokens = registrationTokens,
Data = new Dictionary<string, string>()
{
{ "score", "850" },
{ "time", "2:45" },
},
};

var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message);
if (response.FailureCount > 0)
{
var failedTokens = new List<string>();
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]
Expand Down
6 changes: 3 additions & 3 deletions FirebaseAdmin/FirebaseAdmin/Messaging/SendResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ private SendResponse(FirebaseException exception)

/// <summary>
/// Gets a value indicating whether the send operation was successful or not. When this property
/// is <see langword="true"/>, <see cref="MessageId"/> is guaranteed to return a
/// non-<see langword="null"/> value. When this property is <see langword="false"/>,
/// <see cref="Exception"/> is guaranteed to return a non-<see langword="null"/> value.
/// is <c>true</c>, <see cref="MessageId"/> is guaranteed to return a
/// non-null value. When this property is <c>false</c>,
/// <see cref="Exception"/> is guaranteed to return a non-null value.
/// </summary>
public bool IsSuccess => !string.IsNullOrEmpty(this.MessageId);

Expand Down