From a07d323a37583553b757e9a345301a28a9d61a6b Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Fri, 22 Jul 2016 11:47:38 +0200 Subject: [PATCH] receive the GCM implements a GCM receiver service --- .../BookAStar.Droid/BookAStar.Droid.csproj | 1 + .../Services/GcmListenerService.cs | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 BookAStar/BookAStar.Droid/Services/GcmListenerService.cs diff --git a/BookAStar/BookAStar.Droid/BookAStar.Droid.csproj b/BookAStar/BookAStar.Droid/BookAStar.Droid.csproj index 6ee03fb3..3e33b707 100644 --- a/BookAStar/BookAStar.Droid/BookAStar.Droid.csproj +++ b/BookAStar/BookAStar.Droid/BookAStar.Droid.csproj @@ -191,6 +191,7 @@ + diff --git a/BookAStar/BookAStar.Droid/Services/GcmListenerService.cs b/BookAStar/BookAStar.Droid/Services/GcmListenerService.cs new file mode 100644 index 00000000..7033ba05 --- /dev/null +++ b/BookAStar/BookAStar.Droid/Services/GcmListenerService.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using Android.App; +using Android.Content; +using Android.OS; +using Android.Runtime; +using Android.Views; +using Android.Widget; + +namespace BookAStar.Droid.Services +{ + using Android.App; + using Android.Content; + using Android.OS; + using Android.Gms.Gcm; + using Android.Util; + + namespace ClientApp + { + [Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })] + public class MyGcmListenerService : GcmListenerService + { + public override void OnMessageReceived(string from, Bundle data) + { + var message = data.GetString("message"); + Log.Debug("MyGcmListenerService", "From: " + from); + Log.Debug("MyGcmListenerService", "Message: " + message); + SendNotification(message); + } + + void SendNotification(string message) + { + var intent = new Intent(this, typeof(MainActivity)); + intent.AddFlags(ActivityFlags.ClearTop); + var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot); + + var notificationBuilder = new Notification.Builder(this) + .SetSmallIcon(Resource.Drawable.icon) + .SetContentTitle("GCM Message") + .SetContentText(message) + .SetAutoCancel(true) + .SetContentIntent(pendingIntent); + + var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService); + notificationManager.Notify(0, notificationBuilder.Build()); + } + } + } +} \ No newline at end of file