Created
March 13, 2021 18:33
-
-
Save agehlot/9b379d100f9e38bd500112cff8449417 to your computer and use it in GitHub Desktop.
Firebase push notification service
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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Net; | |
using System.Text; | |
using System.Web.Script.Serialization; | |
namespace Core.Foundation.Notification.Services | |
{ | |
[Service(typeof(IPushNotificationService))] | |
public class PushNotificationService : IPushNotificationService | |
{ | |
/// <summary> | |
/// The log service | |
/// </summary> | |
private readonly ILogService _logService; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="PushNotificationService"/> class. | |
/// </summary> | |
/// <param name="logService">The log service.</param> | |
public PushNotificationService(ILogService logService) | |
{ | |
_logService = logService; | |
} | |
/// <summary> | |
/// Sends the push notification. | |
/// </summary> | |
/// <param name="title">The title.</param> | |
/// <param name="body">The body.</param> | |
/// <param name="token">The token.</param> | |
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> | |
public bool SendPushNotification(string title, string body, List<string> token) | |
{ | |
try | |
{ | |
var applicationID = NotificationAppSetting.ApplicationID; | |
var senderId = NotificationAppSetting.SenderID; | |
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); | |
tRequest.Method = "post"; | |
tRequest.ContentType = "application/json"; | |
var data = new | |
{ | |
registration_ids = token, | |
notification = new | |
{ | |
title = title, | |
body = body | |
} | |
}; | |
var serializer = new JavaScriptSerializer(); | |
var json = serializer.Serialize(data); | |
Byte[] byteArray = Encoding.UTF8.GetBytes(json); | |
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID)); | |
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId)); | |
tRequest.ContentLength = byteArray.Length; | |
using (Stream dataStream = tRequest.GetRequestStream()) | |
{ | |
dataStream.Write(byteArray, 0, byteArray.Length); | |
using (WebResponse tResponse = tRequest.GetResponse()) | |
{ | |
using (Stream dataStreamResponse = tResponse.GetResponseStream()) | |
{ | |
using (StreamReader tReader = new StreamReader(dataStreamResponse)) | |
{ | |
String sResponseFromServer = tReader.ReadToEnd(); | |
_logService.Info($"ServerResponse:{sResponseFromServer}"); | |
} | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
_logService.Error($"Core.Foundation.Notification.Services.PushNotification: {ex.StackTrace}"); | |
return false; | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment