16 ratings
  • 1
  • 2
  • 3
  • 4
  • 5
5 star 6
4 star 9
3 star 1
2 star 0
1 star 0
Compatible with
  • Android

Google Cloud Messaging Client 1.0

Redth

Add Google Cloud Messaging to your Xamarin.Android application

Google Cloud Messaging Client

GCM (Google Cloud Messaging) is a component that makes getting your Xamarin.Android app setup to receive Push Notifications quickly and easily!

Registering for GCM Push Notifications

//Check to see that GCM is supported and that the manifest has the correct information
GcmClient.CheckDevice(this);
GcmClient.CheckManifest(this);

//Call to Register the device for Push Notifications
GcmClient.Register(this, GcmBroadcastReceiver.SENDER_IDS);

Service Implementation

Add these classes to your own project, and be sure to update the SENDER_IDS with your own Sender ID(s) from your Google API Console Project(s):

[BroadcastReceiver(Permission=Constants.PERMISSION_GCM_INTENTS)]
[IntentFilter(new string[] { Constants.INTENT_FROM_GCM_MESSAGE }, 
    Categories = new string[] { "@PACKAGE_NAME@" })]
[IntentFilter(new string[] { Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK }, 
    Categories = new string[] { "@PACKAGE_NAME@" })]
[IntentFilter(new string[] { Constants.INTENT_FROM_GCM_LIBRARY_RETRY }, 
    Categories = new string[] { "@PACKAGE_NAME@" })]
public class GcmBroadcastReceiver : GcmBroadcastReceiverBase<GcmService>
{
    //IMPORTANT: Change this to your own Sender ID!
    //The SENDER_ID is your Google API Console App Project Number
    public static string[] SENDER_IDS = new string[] {"697360970929"};
}

[Service] //Must use the service tag
public class GcmService : GcmServiceBase
{
    public GcmService() : base(GcmBroadcastReceiver.SENDER_IDS) { }

    protected override void OnRegistered (Context context, string registrationId)
    {
        //Receive registration Id for sending GCM Push Notifications to
    }

    protected override void OnUnRegistered (Context context, string registrationId)
    {
        //Receive notice that the app no longer wants notifications
    }

    protected override void OnMessage (Context context, Intent intent)
    {
        //Push Notification arrived - print out the keys/values
        if (intent == null || intent.Extras == null)
            foreach (var key in intent.Extras.KeySet())
                Console.WriteLine("Key: {0}, Value: {1}");
    }

    protected override bool OnRecoverableError (Context context, string errorId)
    {
        //Some recoverable error happened
    }

    protected override void OnError (Context context, string errorId)
    {
        //Some more serious error happened
    }
}

Changes

v1.0

  • First release
Write a Review

7 reviews

Raj Mouli

Messages may arrive while the the application is not running. The following code snippet is another example of a BroadcastReciever subclass that will respond to messages when the application starts:

[BroadcastReceiver] [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })] public class MyGCMBootReceiver : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { MyIntentService.RunIntentInService(context, intent); SetResult(Result.Ok, null, null); } }

Posted on: November 5 / Version: 1.0
giancarlo lallopizzi rates this with
  • 1
  • 2
  • 3
  • 4
  • 5

It simply does what it says to do

Posted on: September 12 / Version: 1.0
Rupert Rawnsley rates this with
  • 1
  • 2
  • 3
  • 4
  • 5

Does what it says on the tin. Might I suggest the following:

  • Update the boilerplate code to include a "return true/false" in the OnRecoverableError (true means try and register again) so it compiles out-of-the-box
  • Try / Catch around the initial registration code as an exception is the only way you know they have failed
  • A note about the (necessary) permissions that are added silently as they might cause confusion to developers later on, especially those of us who expect all permissions to come from AndroidManifest.xml
Posted on: July 2 / Version: 1.0
Grigory Andrianov rates this with
  • 1
  • 2
  • 3
  • 4
  • 5

Thanks for the component!

//Push Notification arrived - print out the keys/values

    if (intent == null || intent.Extras == null)

        foreach (var key in intent.Extras.KeySet())

            Console.WriteLine("Key: {0}, Value: {1}");

It's not right.

Posted on: June 15 / Version: 1.0
Martin Rojas rates this with
  • 1
  • 2
  • 3
  • 4
  • 5

In the example provided, the app has to be executing to receive the notifications.

Is there another approach in which i can receive the notification even when the app is closed? Or i'm into a limitation within the Mono runtime?

Posted on: June 6 / Version: 1.0
Andrew Arnott rates this with
  • 1
  • 2
  • 3
  • 4
  • 5

Works really well. I didn't realize how easy it made it until I saw the code you have to write without this. It's significantly harder.

I wish the message handler allowed for async execution (while keeping the wake lock).

Posted on: December 27, 2013 / Version: 1.0
Alberto Piccinin

Hi,

is it possible to send Notification to a single Device?

Posted on: December 10, 2013 / Version: 1.0