|
14 | 14 |
|
15 | 15 | using System; |
16 | 16 | using System.Collections.Generic; |
| 17 | +using System.Linq; |
17 | 18 | using System.Threading.Tasks; |
18 | 19 | using FirebaseAdmin.Messaging; |
19 | 20 |
|
@@ -117,6 +118,107 @@ internal static async Task SendDryRunAsync() |
117 | 118 | // [END send_dry_run] |
118 | 119 | } |
119 | 120 |
|
| 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 | + |
120 | 222 | internal static Message CreateAndroidMessage() |
121 | 223 | { |
122 | 224 | // [START android_message] |
|
0 commit comments