Xamarin Android FCM Push
This commit is contained in:
70
MyFirebaseMessagingService.cs
Normal file
70
MyFirebaseMessagingService.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using Android.Media;
|
||||
using Android.Net;
|
||||
using Android.Support.V4.App;
|
||||
using Android.Util;
|
||||
using Firebase.Messaging;
|
||||
using Plugin.Badge;
|
||||
|
||||
namespace FCMTest
|
||||
{
|
||||
[Service]
|
||||
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
|
||||
class MyFirebaseMessagingService : FirebaseMessagingService
|
||||
{
|
||||
const string TAG = "FCMTest MessageService";
|
||||
|
||||
int m_iNotifyNo = 0;
|
||||
|
||||
private void SendLocalNotification(string title, string body)
|
||||
{
|
||||
|
||||
|
||||
Intent intent = new Intent(this, Java.Lang.Class.FromType(typeof(MainActivity)));
|
||||
intent.AddFlags(ActivityFlags.ClearTop);
|
||||
PendingIntent pendingIntent = PendingIntent.GetActivity(this, m_iNotifyNo, intent, PendingIntentFlags.OneShot);
|
||||
if(m_iNotifyNo < 9)
|
||||
m_iNotifyNo++;
|
||||
else
|
||||
m_iNotifyNo = 0;
|
||||
Uri defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
|
||||
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
|
||||
.SetSmallIcon(Resource.Drawable.eyulchon)
|
||||
.SetContentTitle(title)
|
||||
.SetContentText(body)
|
||||
.SetAutoCancel(true)
|
||||
.SetSound(defaultSoundUri)
|
||||
.SetContentIntent(pendingIntent);
|
||||
|
||||
NotificationManager notificationManager =
|
||||
(NotificationManager)GetSystemService(Context.NotificationService);
|
||||
|
||||
notificationManager.Notify(m_iNotifyNo+2, notificationBuilder.Build());
|
||||
}
|
||||
|
||||
public override void OnMessageReceived(RemoteMessage message)
|
||||
{
|
||||
Log.Debug(TAG, "onMessageReceived");
|
||||
|
||||
//base.OnMessageReceived(message);
|
||||
|
||||
if(message.Data.Count > 0)
|
||||
{
|
||||
Log.Debug(TAG, "message data payload: " + message.Data);
|
||||
|
||||
string title = message.Data.ContainsKey("title") ? message.Data["title"] : "";
|
||||
string body = message.Data.ContainsKey("body") ? message.Data["body"] : "";
|
||||
SendLocalNotification(title, body);
|
||||
|
||||
string strBadge = message.Data.ContainsKey("badge") ? message.Data["badge"] : "0";
|
||||
int iBadge;
|
||||
int.TryParse(strBadge, out iBadge);
|
||||
CrossBadge.Current.SetBadge(iBadge);
|
||||
}
|
||||
|
||||
if(message.GetNotification() != null)
|
||||
Log.Debug(TAG, "Message Notification Body: " + message.GetNotification().Body);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user