CloudBuilder  2.8.4
Making social games is easy !
Push Notification

Table of Content

  1. Push Notification for iOS
  2. Push Notification for Android

Setup Notification for iOS

First, setup a certificate with Push notification enabled on Apple Developer Center

ios-push-1.png
Create Certificate

Download it,

ios-push-2.png
Download Certificate

and convert it to .pem format with the following command :

1 openssl x509 -in certificat.cer -inform DER -outform PEM -out certificat-aps-dev

You'll also need to convert the private key used to generate this certificate to the .pem format. To do this, you first need to export your private key to p12 format by using the Keychain Access tool on your Mac. Launch Keychain Access and right click on the certificate that you have just downloaded and installed, and select the "Export" option, and then the .p12 format.

Keychain_Access.png
Export your private key

Once this is done, just convert your private key to .pem format with the following command:

1 openssl pkcs12 -in private_push_key.p12 -out push-key-dev.pem -nodes

Once you've got your two .pem files, open a web page to your Back Office web page, tick the iOS box inside the the "Push Notification Certificates" in your Status page, and fill respectively the "Certificate" and "Private Key" cells with the contents of the files you just created at the previous step (see above).

CotC_iOS_PushNotifs.png
Setting up your iOS push notifications with Clan of the Cloud back office

Finally, a little piece of code is needed in your AppDelegate.mm file :

1 #include "CClan.h"
2 
3 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
4 {
5  CloudBuilder::AchieveRegisterDevice(deviceToken.length, deviceToken.bytes);
6 }
7 
8 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
9 {
10  NSLog(@"Error in registration. Error: %@ : %@", error, [error localizedDescription]);
11 }
12 
13 // Handle an actual notification
14 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
15 {
16  // ...
17 
18  // this one is needed to reset the bagde number on the App Icon
19  application.applicationIconBadgeNumber = 0;
20 }

Setup Notification for Android

In case you need some more information about how Google Cloud Messaging works, please have a look at the official Google documentation. Otherwise, by following the steps below, you should be able to have your application manage push notifications pretty quickly.

First, you need to setup a Google Application. The necessary steps to do this are explained in details in the Google+ section. Once this is done, this Google Application needs to reference the Google Cloud Messaging for Android API. This is done from the APIs section of the Google Developer Console for your project.

Google_Messaging_Android.png
Activating Google Cloud Messaging

Next step is to create a Public API key. To do so, select the Credentials section, just below the APIs section in the previous step, scroll down to the bottom of the page, and click the "Create new Key" button for the Public API access list of keys. Then select "Server key" option, and then "Create", not modifying anything. You should now have a new, valid API key.

Google_Public_API_Access.png
Creating a new Server key

Once this is done, you're almost finished with parameterization, and only some bits of code will be needed. But first, you need to declare your Google Application with Clan of the Cloud through your Back Office web page. Now it's straightforward. Just tick the Android box inside the "Push Notification Certificates" in your Status page, and fill the "senderID" cell with the Google Application project number, and the "apiKey" cell with the key you just created at the previous step (see above).

CotC_Android_PushNotifs.png
Setting up your Android push notifications with Clan of the Cloud back office

Now everything's been done on the Google Application side, we need to add a few snippets of code, so your application can communicate with the Google Cloud Messaging servers. First thing to modify is your AndroidManifest.xml, which needs a few new sections. Each time you see "com.yourcompany.yourapp", you need to replace that with the value of the package attribute from your manifest.

Inside the <manifest> tag, make sure to have the following entries:

1 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
2 <uses-permission android:name="android.permission.GET_ACCOUNTS" />
3 <uses-permission android:name="android.permission.WAKE_LOCK" />
4 
5 <permission android:name="com.yourcompany.yourapp.permission.C2D_MESSAGE" android:protectionLevel="signature" />
6 <uses-permission android:name="com.yourcompany.yourapp.permission.C2D_MESSAGE" />
7 <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

Inside the <application> tag, make sure to have the following entries:

1 <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
2  <intent-filter>
3 
4  <action android:name="com.google.android.c2dm.intent.RECEIVE" />
5 
6  <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
7  <category android:name="com.yourcompany.yourapp" />
8  </intent-filter>
9 </receiver>
10 
11 <service android:name=".GCMIntentService" android:enabled="true" />

Your application is now ready to receive push notifications from Google Cloud Messaging. We just need to modify the Activity so these push notifications are actually processed and in turn invoke Clan of the Cloud program loop. Your activity Java file has to be modified so it includes the following content:

1 package com.yourcompany.yourapp;
2 
3 import android.content.BroadcastReceiver;
4 import android.content.Intent;
5 import android.content.IntentFilter;
6 
7 import com.yourcompany.yourapp.GCMIntentService;
8 import com.clanofthecloud.cloudbuilder.CloudBuilder;
9 
10 public class YourActivity extends Activity
11 {
12  ...
13  IntentFilter gcmFilter;
14  ...
15 
16  @Override
17  public void onCreate(Bundle savedInstanceState)
18  {
19  GCMIntentService.Init(this);
20  gcmFilter = new IntentFilter();
21  gcmFilter.addAction(GCMIntentService.NotificationName);
22  }
23 
24  ...
25 
26  private final BroadcastReceiver gcmReceiver = new BroadcastReceiver()
27  {
28  @Override
29  public void onReceive(Context context, Intent intent)
30  {
31  GCMIntentService.HandleNotification(intent);
32  }
33  };
34 
35  ...
36 
37  @Override
38  public synchronized void onResume()
39  {
40  super.onResume();
41  ...
42  registerReceiver(gcmReceiver, gcmFilter);
43  CloudBuilder.Resumed();
44  CloudBuilder.HandleNotification(null);
45  ...
46  }
47 
48  ...
49 
50  public synchronized void onPause()
51  {
52  super.onPause();
53  ...
54  CloudBuilder.Suspended();
55  unregisterReceiver(gcmReceiver);
56  ...
57  }
58 }

Finally, you need to customize the generic GCMIntentService Java file that you have received with the Android sample:

1 package com.yourcompany.yourapp;
2 
3 
4 import android.content.Context;
5 import android.content.Intent;
6 
7 import com.clanofthecloud.cloudbuilder.CloudBuilder;
8 
9 public class GCMIntentService extends CloudBuilder {
10 
11  // SenderID should contain the Project Number, as found in the Google Developer Console when you
12  // you created the Google Application.
13  public static String SenderID = "xxxxxxxxxxxx";
14  // This needs to be unique, so you can use your package name, and must end with GCM_RECEIVED_ACTION.
15  public static String NotificationName = "com.yourcompany.yourapp.GCM_RECEIVED_ACTION";
16 
17 
18  public static void Init (Context context) {
19  CloudBuilder.Init(context, SenderID, null);
20  }
21 
22  @Override
23  public int GetNotificationIcon() {
24  // Should point to an icon in your project resources, which will be used
25  // to decorate the push notification.
26  return R.drawable.ic_launcher;
27  }
28 
29  @Override
30  public String GetNotificationTitle() {
31  // Should contain the string you want to see displayed when the push
32  // notification is presented on your device.
33  return "CLI notification";
34  }
35 
36  @Override
37  public Intent GetIntent(Context context)
38  {
39  return new Intent(context, MicroConsole.class);
40  }
41 
42  @Override
43  public String GetNotificationName() {
44  return NotificationName;
45  }
46 
47 }

That's it, you're done and can now begin to send push notifications to your Android application!