Skip to content

Commit 44d3f51

Browse files
authored
Merge pull request #40 from firebase/hkj-multicast-snippets
Snippets for FCM multicast API
2 parents ae0d725 + e419b44 commit 44d3f51

File tree

3 files changed

+107
-4
lines changed

3 files changed

+107
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

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

56
# v1.2.1
67

FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseMessagingSnippets.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17+
using System.Linq;
1718
using System.Threading.Tasks;
1819
using FirebaseAdmin.Messaging;
1920

@@ -117,6 +118,107 @@ internal static async Task SendDryRunAsync()
117118
// [END send_dry_run]
118119
}
119120

121+
internal static async Task SendAllAsync()
122+
{
123+
var registrationToken = "YOUR_REGISTRATION_TOKEN";
124+
// [START send_all]
125+
// Create a list containing up to 100 messages.
126+
var messages = new List<Message>()
127+
{
128+
new Message()
129+
{
130+
Notification = new Notification()
131+
{
132+
Title = "Price drop",
133+
Body = "5% off all electronics",
134+
},
135+
Token = registrationToken,
136+
},
137+
new Message()
138+
{
139+
Notification = new Notification()
140+
{
141+
Title = "Price drop",
142+
Body = "2% off all books",
143+
},
144+
Topic = "readers-club",
145+
},
146+
};
147+
148+
var response = await FirebaseMessaging.DefaultInstance.SendAllAsync(messages);
149+
// See the BatchResponse reference documentation
150+
// for the contents of response.
151+
Console.WriteLine($"{response.SuccessCount} messages were sent successfully");
152+
// [END send_all]
153+
}
154+
155+
internal static async Task SendMulticastAsync()
156+
{
157+
// [START send_multicast]
158+
// Create a list containing up to 100 registration tokens.
159+
// These registration tokens come from the client FCM SDKs.
160+
var registrationTokens = new List<string>()
161+
{
162+
"YOUR_REGISTRATION_TOKEN_1",
163+
// ...
164+
"YOUR_REGISTRATION_TOKEN_n",
165+
};
166+
var message = new MulticastMessage()
167+
{
168+
Tokens = registrationTokens,
169+
Data = new Dictionary<string, string>()
170+
{
171+
{ "score", "850" },
172+
{ "time", "2:45" },
173+
},
174+
};
175+
176+
var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message);
177+
// See the BatchResponse reference documentation
178+
// for the contents of response.
179+
Console.WriteLine($"{response.SuccessCount} messages were sent successfully");
180+
// [END send_multicast]
181+
}
182+
183+
internal static async Task SendMulticastAndHandleErrorsAsync()
184+
{
185+
// [START send_multicast_error]
186+
// These registration tokens come from the client FCM SDKs.
187+
var registrationTokens = new List<string>()
188+
{
189+
"YOUR_REGISTRATION_TOKEN_1",
190+
// ...
191+
"YOUR_REGISTRATION_TOKEN_n",
192+
};
193+
var message = new MulticastMessage()
194+
{
195+
Tokens = registrationTokens,
196+
Data = new Dictionary<string, string>()
197+
{
198+
{ "score", "850" },
199+
{ "time", "2:45" },
200+
},
201+
};
202+
203+
var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message);
204+
if (response.FailureCount > 0)
205+
{
206+
var failedTokens = new List<string>();
207+
for (var i = 0; i < response.Responses.Count; i++)
208+
{
209+
if (!response.Responses[i].IsSuccess)
210+
{
211+
// The order of responses corresponds to the order of the registration tokens.
212+
failedTokens.Add(registrationTokens[i]);
213+
}
214+
}
215+
216+
Console.WriteLine($"List of tokens that caused failures: {failedTokens}");
217+
}
218+
219+
// [END send_multicast_error]
220+
}
221+
120222
internal static Message CreateAndroidMessage()
121223
{
122224
// [START android_message]

FirebaseAdmin/FirebaseAdmin/Messaging/SendResponse.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ private SendResponse(FirebaseException exception)
4444

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

0 commit comments

Comments
 (0)