Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM (Firebase Cloud Messaging) Push Notification with Asp.Net

Tags:

FCM

I have already push the GCM message to google server using asp .net in following method,

GCM Push Notification with Asp.Net

Now i have planned upgrade to FCM method, anyone have idea about this or developing this in asp .net let me know..

like image 736
bgs Avatar asked Jul 04 '16 12:07

bgs


1 Answers

2019 Update

There's a new .NET Admin SDK that allows you to send notifications from your server. Install via Nuget

Install-Package FirebaseAdmin 

You'll then have to obtain the service account key by downloading it by following the instructions given here, and then reference it in your project. I've been able to send messages by initializing the client like this

using FirebaseAdmin; using FirebaseAdmin.Messaging; using Google.Apis.Auth.OAuth2; ...  public class MobileMessagingClient : IMobileMessagingClient {     private readonly FirebaseMessaging messaging;      public MobileMessagingClient()     {         var app = FirebaseApp.Create(new AppOptions() { Credential = GoogleCredential.FromFile("serviceAccountKey.json").CreateScoped("https://www.googleapis.com/auth/firebase.messaging")});                    messaging = FirebaseMessaging.GetMessaging(app);     }     //...           } 

After initializing the app you are now able to create notifications and data messages and send them to the devices you'd like.

private Message CreateNotification(string title, string notificationBody, string token) {         return new Message()     {         Token = token,         Notification = new Notification()         {             Body = notificationBody,             Title = title         }     }; }  public async Task SendNotification(string token, string title, string body) {     var result = await messaging.SendAsync(CreateNotification(title, body, token));      //do something with result } 

..... in your service collection you can then add it...

services.AddSingleton<IMobileMessagingClient, MobileMessagingClient >(); 
like image 175
Alfred Waligo Avatar answered Sep 20 '22 22:09

Alfred Waligo