Created
September 16, 2018 03:36
-
-
Save iseebi/4df63919453ff404b7abd674fe2a3cf1 to your computer and use it in GitHub Desktop.
FCM(Firebase Cloud Messaging) Mock with Azure Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "Newtonsoft.Json" | |
using System.Net; | |
using System.Text; | |
using Newtonsoft.Json; | |
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) | |
{ | |
var pairs = req.GetQueryNameValuePairs(); | |
var notRegistered = int.Parse(pairs.FirstOrDefault(q => string.Compare(q.Key, "notregistered", true) == 0) | |
.Value ?? "0"); | |
var canonical = int.Parse( pairs.FirstOrDefault(q => string.Compare(q.Key, "canonical", true) == 0) | |
.Value ?? "0"); | |
var failure = int.Parse(pairs.FirstOrDefault(q => string.Compare(q.Key, "failure", true) == 0) | |
.Value ?? "0"); | |
dynamic data = await req.Content.ReadAsAsync<object>(); | |
var results = new List<object>(); | |
var i = 0; | |
var successCount = 0; | |
var failureCount = 0; | |
var canonicalCount = 0; | |
foreach (var id in data.registration_ids) { | |
if (i < notRegistered) { | |
results.Add(new { error = "NotRegistered" }); | |
failureCount++; | |
} | |
else if (i < (notRegistered + canonical)) { | |
results.Add(new { message_id = "123:" + i.ToString(), registration_id = "canonical_" + i.ToString() }); | |
canonicalCount++; | |
successCount++; | |
} | |
else if (i < (notRegistered + canonical + failure)) { | |
results.Add(new { error = "Unavailable" }); | |
failureCount++; | |
} | |
else { | |
results.Add(new { message_id = "123:" + i.ToString() }); | |
successCount++; | |
} | |
i++; | |
} | |
var jsonToReturn = JsonConvert.SerializeObject(new { | |
multicast_id = 1234, | |
success = successCount, | |
failure = failureCount, | |
canonical_ids = canonicalCount, | |
results = results | |
}); | |
return new HttpResponseMessage(HttpStatusCode.OK) { | |
Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json") | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment