-
Notifications
You must be signed in to change notification settings - Fork 1.5k
How to Configure & Send GCM Google Cloud Messaging Push 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
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:
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.
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.
To obtain an API key:
- In the main Google APIs Console page, select API Access. You will see a screen that resembles the following:
- Click Create new Server key. The following screen appears:
- Click Create: Take note of the API key value (YourKeyWillBeShownHere) in this example, as it will be used later on.
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.
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 service
PushService push = new PushService();
//Wire up the events
push.Events.OnDeviceSubscriptionExpired += new Common.ChannelEvents.DeviceSubscriptionExpired(Events_OnDeviceSubscriptionExpired);
push.Events.OnDeviceSubscriptionIdChanged += new Common.ChannelEvents.DeviceSubscriptionIdChanged(Events_OnDeviceSubscriptionIdChanged);
push.Events.OnChannelException += new Common.ChannelEvents.ChannelExceptionDelegate(Events_OnChannelException);
push.Events.OnNotificationSendFailure += new Common.ChannelEvents.NotificationSendFailureDelegate(Events_OnNotificationSendFailure);
push.Events.OnNotificationSent += new Common.ChannelEvents.NotificationSentDelegate(Events_OnNotificationSent);
//Configure and start Android GCM
//IMPORTANT: The SENDER_ID is your Google API Console App Project ID.
// Be sure to get the right Project ID from your Google APIs Console. It's not the named project ID that appears in the Overview,
// but instead the numeric project id in the url: eg: https://code.google.com/apis/console/?pli=1#project:785671162406:overview
// where 785671162406 is the project id, which is the SENDER_ID to use!
push.StartGoogleCloudMessagingPushService(
new GcmPushChannelSettings("<Sender ID>", "<API Access API Key>", "<Android App Package Name>"));
//Fluent construction of an Android GCM Notification
push.QueueNotification(NotificationFactory.AndroidGcm()
.ForDeviceRegistrationId("<Android App GCM Registration ID>")
.WithCollapseKey("NONE")
.WithJson("{\\"alert\\":\\"Alert Text!\\",\\"badge\\":\\"7\\"}"));
Console.WriteLine("Waiting for Queue to Finish...");
//Stop and wait for the queues to drains
push.StopAllServices(true);