Skip to content

How to Configure & Send GCM Google Cloud Messaging Push Notifications using PushSharp

Redth edited this page Mar 19, 2013 · 6 revisions

How to Configure & Send Google Cloud Messaging Notifications using PushSharp

This is a step by step picture guide that will show you how to set up your Android App to be enabled for sending push notifications using GCM (Google Cloud Messaging) services, and how to configure your PushSharp code to send out push notifications.

Parts of this guide were taken from Google's documentation which you can find here: http://developer.android.com/guide/google/gcm/index.html

Creating a Google API project

To create a Google API project:

  • Open the Google APIs Console page.
  • If you haven't created an API project yet, this page will prompt you to do so:

Create Project

Note: If you already have existing projects, the first page you see will be the Dashboard page. From there you can create a new project by opening the project drop-down menu (upper left corner) and choosing Other projects > Create.

  • Click Create project. Your browser URL will change to something like:

https://code.google.com/apis/console/#project:4815162342

  • Take note of the value after #project: (4815162342 in this example). This is your project ID, and it will be used later on as the GCM sender ID.

Enabling the GCM Service

To enable the GCM service:

  • In the main Google APIs Console page, select Services.
  • Turn the Google Cloud Messaging toggle to ON.
  • In the Terms of Service page, accept the terms.

Obtaining an API Key

To obtain an API key:

  • In the main Google APIs Console page, select API Access. You will see a screen that resembles the following:

API Access

  • Click Create new Server key. The following screen appears:

Create New Server Key

  • Click Create: Take note of the API key value (YourKeyWillBeShownHere) in this example, as it will be used later on.

Create

Note: If you need to rotate the key, click Generate new key. A new key will be created while the old one will still be active for up to 24 hours. If you want to get rid of the old key immediately (for example, if you feel it was compromised), click Delete key.

Setting up PushSharp to Send Notifications

Once you have your:

  • Sender ID (eg: 4815162342)
  • API Access API Key (eg:
  • Android Application Package Name (eg: com.pushsharp.test)
  • One or more Android App GCM device Registration ID's for the devices you want to send Notifications to (see the wiki for how to obtain this in a Mono for Android Application)

You are ready to start sending notifications! Take a look at the following code, and be sure to substitue in your own values where specified:

//Create our push services broker
var push = new PushBroker();

//Wire up the events for all the services that the broker registers
push.OnNotificationSent += NotificationSent;
push.OnChannelException += ChannelException;
push.OnServiceException += ServiceException;
push.OnNotificationFailed += NotificationFailed;
push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
push.OnChannelCreated += ChannelCreated;
push.OnChannelDestroyed += ChannelDestroyed;


push.RegisterGcmService(new GcmPushChannelSettings("YOUR GCM AUTHORIZATION KEY HERE"));

push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("DEVICE REGISTRATION ID HERE")
                      .WithJson("{\"alert\":\"Hello World!\",\"badge\":7,\"sound\":\"sound.caf\"}"));


//Stop and wait for the queues to drains
push.StopAllServices();