Using Google Cloud Messaging (GCM) on Android and iOS in Xamarin

Assuming you've already got a Xamarin Forms project set up and ready to go, we'll begin with installing the Xamarin GCM service into your app and then talk about adding the code to interface with it.

Installing GCM:

You'll want to begin by right clicking the Portable or Shared project within the solution and clicking 'Manage NuGet Packages'. Once open, you'll need to click 'Browse' at the top left and then search for 'Xamarin.GooglePlayServices.Gcm' and install the package. Note: Make sure you've got the correct package, the 'Author' label displays as 'Xamarin Inc'. You'll want to repeat this process for both the Android and iOS projects in the solution.

Before you link GCM to your app, you'll need to make Google aware of it by going to the Google Developer Console and creating your app, then adding GCM to it. You might find this page useful for helping you create it: http://dev.tapjoy.com/faq/how-to-find-sender-id-and-api-key-for-gcm/

Android:

Within your 'AndroidManifest.xml' file add the following permissions to enable your app to receive notifications:

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="{MYAPPNAMESPACE}.permission.C2D_MESSAGE" />

Make sure to add them above <application...>...</application>. And replace {MYAPPNAMESPACE} with the root namespace that your app runs in.

Then add the following within the application tags:

<receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
    <category android:name="bifa.droid" />
  </intent-filter>
</receiver>

This tells the Android phone to hook in a receiver to look for information sent by GCM, you'll want to create a file called GCMListenerService.cs and add it to it.

namespace {MYAPPNAMESPACE}
{
    [Service (Exported = false), IntentFilter (new [] { "com.google.android.c2dm.intent.RECEIVE" })]
    public class MyGcmListenerService : GcmListenerService
    {
        public override void OnMessageReceived (string from, Bundle data)
        {
            string id = data.GetString ("id");
            string title = data.GetString ("title");
            string message = data.GetString ("message");

            Log.Debug ("MyGcmListenerService", "From:    " + from);
            Log.Debug ("MyGcmListenerService", "Message: " + message);

            SendNotification (id, title, message);
        }

        void SendNotification (string id, string title, string message)
        {

                var intent = new Intent (this, typeof(MainActivity));
                intent.AddFlags (ActivityFlags.ClearTop);
                intent.PutExtra("id", int.Parse(id));
                var pendingIntent = PendingIntent.GetActivity (this, 0, intent, PendingIntentFlags.OneShot);

                var notificationBuilder = new Notification.Builder(this)
                    .SetSmallIcon (Resource.Drawable.icon)
                    .SetVibrate(new long[] { 0, 200 })
                    .SetContentTitle (title)
                    .SetContentText (message)
                    .SetAutoCancel (true)
                    .SetContentIntent (pendingIntent);

                var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                notificationManager.Notify (0, notificationBuilder.Build());
        }
    }
}

 

iOS:

 


By Luke Alderton at 1 Jul 2016, 12:45 PM

Tags: Xamarin,Android,iOS

Comments

Zane Campbell
Why does this article just end? It is definitely not completed. Nothing for iOS.
06/01/2017
Post a comment
Sit tight...
We're adding your comment.
Thank you.
Your comment has been added.
There's been a problem.
Please try again later.
Share with
Tags
Latest Comments
By dipbleds on Raspberry Pi - Bluetooth using Bluecove on Raspbian
12 Dec 2017, 21:29 PM
By Ich on Replacing the Xamarin Header/Navigation bar with a custom view/template
26 Nov 2017, 17:11 PM
By Ravi Motha on My experience at Umbraco UK Festival 2017
12 Dec 2017, 08:39 AM
By Borges on How to add a Xamarin Forms Loading Screen/Overlay
7 Nov 2017, 19:11 PM
By Rutul Mehta on Auto/Custom height on Xamarin Forms WebView for Android and iOS
4 Nov 2017, 03:15 AM
By Pablo on Xamarin MasterDetailPage has large margin/padding at top
26 Oct 2017, 15:31 PM
By faiza on Using MaryTTS or OpenMary in Java
22 Oct 2017, 12:54 PM
By Nietoperz on Using MaryTTS or OpenMary in Java
29 Sep 2017, 14:34 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development