Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Push Notification (FCM) using C#

I have a REST Api written in .NET Core, now has a requirement to create a Push Notification to Firebase Cloud Messaging (FCM). For testing, am using the Firebase Console but I need to get this done programtically. I have gone through the documentation of Firebase and some examples via Google but am more confused.

I think it is possible to create a message via a regular Http but can someone post a simple working example so that I can pick it up, please? Or maybe, my understanding is totally wrong?

like image 344
Coder Absolute Avatar asked Jul 10 '17 09:07

Coder Absolute


People also ask

How to Generate FCM token in Android?

Access the device registration tokenOn initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token by extending FirebaseMessagingService and overriding onNewToken .


2 Answers

Some people have also liked this question, so thought of providing the solution I implemented, thinking it may help others. Please feel free to ask questions, if you have any.

How to get the Server Key: Here is the question link which helps.

Firebase Cloud Messaging Documentation can be found here.

public class FirebaseNotificationModel
{
    [JsonProperty(PropertyName = "to")]
    public string To { get; set; }

    [JsonProperty(PropertyName = "notification")]
    public NotificationModel Notification { get; set; }
}


using System.Net.Http;
using System.Text;
public static async void Send(FirebaseNotificationModel firebaseModel)
{
    HttpRequestMessage httpRequest = null;
    HttpClient httpClient = null;

    var authorizationKey = string.Format("key={0}", "YourFirebaseServerKey");
    var jsonBody = SerializationHelper.SerializeObject(firebaseModel);

    try
    {
        httpRequest = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send");

        httpRequest.Headers.TryAddWithoutValidation("Authorization", authorizationKey);
        httpRequest.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");

        httpClient = new HttpClient();
        using (await httpClient.SendAsync(httpRequest))
        {
        }
    }
    catch
    {
        throw;
    }
    finally
    {
        httpRequest.Dispose();
        httpClient.Dispose();
    }
}
like image 58
Coder Absolute Avatar answered Sep 21 '22 16:09

Coder Absolute


With .NET Core you can use this lightweight library for FCM push notifications and APN HTTP/2 Push notifications:

Install-Package CorePush

And then for Firebase:

using (var fcm = new FcmSender(serverKey, senderId))
{
    await fcm.SendAsync(deviceToken, notification);
}

Or APN:

using (var apn = new ApnSender(privateKey, keyId, teamId, appbundleId, server)) 
{
    await apn.SendAsync(deviceToken, notification);
}
like image 20
Andrei Avatar answered Sep 21 '22 16:09

Andrei