-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyFirebaseMessagingService.cs
55 lines (49 loc) · 2.25 KB
/
MyFirebaseMessagingService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Android.App;
using Android.Content;
using Android.Support.V4.App;
using Firebase.Messaging;
using System.Collections.Generic;
using Barber.Droid;
using Android.Graphics;
using Xamarin.Forms;
using Barber.Views;
using System;
namespace FCMClient
{
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
var body = message.GetNotification().Body;
var rcvTitle = message.GetNotification().Title;
/*var title = rcvTitle[0];
var type = rcvTitle[1];*/
SendNotification(rcvTitle, body, message.Data);
MessagingCenter.Send<Object>(this, "NewNotification");
}
void SendNotification(string messageTitle, string messageBody, IDictionary<string, string> data)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
foreach (var key in data.Keys)
{
intent.PutExtra(key, data[key]);
}
var pendingIntent = PendingIntent.GetActivity(this,
MainActivity.NOTIFICATION_ID,
intent,
PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.logo)
.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.ic_notification))
.SetContentTitle(messageTitle)
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());
}
}
}