// Notification request to Firebase public async Task <bool> SendNotification(FCMBody fcmBody) { HttpClient client = new HttpClient(); var httpContent = JsonConvert.SerializeObject(fcmBody); // Server key for authorization var authorization = string.Format("key={0}", "AAAA_V-6Iio:APA91bF5-SIIcue9XdaALtO1is8Vlkk2PUVn9Z21LaeYurCI0y0s-1ZNtDA6ZxsMPpzTqM1Lh0uEf1SH-PBMIhOODp6sOY7v7PUOLBqyt-H3PcvbC4-mPmcaUH2Xk52ermtqvNua7vIH"); client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authorization); // Notification content to Firebase API in JSON format StringContent stringContent = new StringContent(httpContent); stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); HttpResponseMessage response = await client.PostAsync("https://fcm.googleapis.com/fcm/send", stringContent); if (response.IsSuccessStatusCode) { return(true); } else { return(false); } }
// Build notification public async Task <bool> HandleNotification() { IsItMyTurnContext context = new IsItMyTurnContext(); string nextApartmentInShift = GetApartmentForNextShift(); // Title and body for notification FCMNotification notification = new FCMNotification(); notification.title = "Leikkuuvuoro vaihtui!"; notification.body = "Seuraavana vuorossa: " + nextApartmentInShift; notification.sound = "default"; // Tokens to array from database which will get the notification string[] fcmTokens = (from ft in context.FcmTokens select ft.Token).ToArray(); List <bool> successList = new List <bool>(); // The notification is sent to device and the result will be added to list of successes foreach (var token in fcmTokens) { FCMBody body = new FCMBody(); body.registration_ids = new string[] { token }; body.notification = notification; bool result = await SendNotification(body); successList.Add(result); } // If all the notification send responses are OK, return true bool allTrue = successList.All(s => Equals(true, s)); if (allTrue) { return(true); } else { return(false); } }