Permalink
Cannot retrieve contributors at this time
Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign up
Find file
Copy path
xamarin-push-demo/XamarinPushDemo.Android/PushNotification/MessagingService.cs
Find file
Copy path
Fetching contributors…

using Android.App; | |
using Android.Content; | |
using Android.Support.V4.App; | |
using Firebase.Messaging; | |
using System.Linq; | |
namespace XamarinPushDemo.Droid.PushNotification | |
{ | |
[Service] | |
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] | |
public class MessagingService : FirebaseMessagingService | |
{ | |
public override void OnMessageReceived(RemoteMessage message) | |
{ | |
// called for every received push notification | |
string title, body; | |
var notification = message.GetNotification(); | |
if (notification == null) | |
{ | |
// azure uses different format (needed when testing from portal | |
// and when sending regular notifications through notification hub) | |
var values = message.Data; | |
body = values.FirstOrDefault(p => p.Key == "message").Value; | |
title = values.FirstOrDefault(p => p.Key == "title").Value; | |
} | |
else | |
{ | |
// firebase console notification (only needed when manually sending notifications via firebase) | |
body = notification.Body; | |
title = notification.Title; | |
} | |
var intent = new Intent(ApplicationContext, typeof(MainActivity)); | |
intent.AddFlags(ActivityFlags.ClearTop); | |
const int id = 1; | |
var pIntent = PendingIntent.GetActivity(ApplicationContext, id, intent, PendingIntentFlags.Immutable); | |
var builder = new NotificationCompat.Builder(ApplicationContext) | |
.SetSmallIcon(Resource.Drawable.logo) | |
.SetContentIntent(pIntent) | |
.SetContentTitle(title) | |
.SetContentText(body) | |
.SetAutoCancel(true); | |
var notificationManager = (NotificationManager)ApplicationContext.GetSystemService(NotificationService); | |
notificationManager.Notify(id, builder.Build()); | |
} | |
} | |
} |