GCM Setup

From Fuseboxx Wiki
Jump to: navigation, search

This page guides you through setting up Google Cloud Messaging for use with the Fuse API as push notifications.

Overview

The Fuse system can send out push notifications alerting users of special offers or perhaps reminding them to return to their game. For general information about Google Cloud Messaging see Here.

To set up a Push Notification, you need to register with Google for GCM Here.

Create a new project to obtain an API key. If you already have a project that uses Google APIs simply enable the GCM and gather the requested information.

Set Up

Step 1: Enable Google Cloud Messaging (Make sure it is turned ON under APIs).

SetupGCM1.png


Step 2: Record the API Key (this can be found under Credentials)


SetupGCM2.png


Step 3: Record the Sender ID (Formally Project Number)

Using the API Key, go Here.

You can now link the API Key to the project being published:

SetupGCM3.png


Which will then create a project key:


SetupGCM4.png

Step 4: Change the AndroidManifest.xml file in your app. Add these lines to the manifest. This allows the FuseAPI to handle incoming GCM.

Replace the ZZZ.ZZZZZZ with the package name (found on the 3rd line of the AndroidManifest.xml file) and YYYYYYYYY with the name of the app. This is the name that appears in the res/values/strings.xml file under app_name.

NoteBubble.png

You must remove any non-alpha-numeric characters from your app name.

.
 <permission android:name="ZZZ.ZZZZZZ.permission.C2D_MESSAGE" android:protectionLevel="signature" />
 <uses-permission android:name="ZZZ.ZZZZZZ.permission.C2D_MESSAGE" /> 
 <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
 <uses-permission android:name="android.permission.INTERNET" /> 
 <uses-permission android:name="android.permission.GET_ACCOUNTS" />
 <uses-permission android:name="android.permission.WAKE_LOCK" />
 
 <receiver android:name="com.fusepowered.push.FusePushNotificationReceiver" 
 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="ZZZ.ZZZZZZ" />
   </intent-filter>
 </receiver>
 <service android:name="com.fusepowered.push.FuseIntentService" />
 
 <activity 
   android:name="com.fusepowered.push.FuseRecordGCMResponse"
   android:configChanges="keyboardHidden|orientation"
   android:label="FuseRecordGCMResponse">
   <intent-filter>
     <action android:name="ZZZ.ZZZZZZ.YYYYYYYYY.fusepowered.push.FuseRecordGCMResponse" />
     <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
 </activity>


Step 5: Enable GCM in your app.

In the integration guide, we provided sample code to initialize the FuseAPI. To enable GCM you must add a couple of extra lines of code. In the following example, when a GCM arrives at the device, Fuse will setup a notification and if the user clicks on that notification it will send them to the start screen. You should create an intent that will land the user somewhere more meaningful.

 public static void startSession(final String gameId,
                                 Activity activity, 
                                 Context ctx, 
                                 FuseCallback callback) { }
 
 // example 
 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
    final Context appContext = getApplicationContext();
 
    final MessageCallback startSessionCallback = new MessageCallback(this) 
    {
       ...
    }
 
    FuseAPI.startSession("<UNIQUE API KEY>",
                         this, 
                         appContext, 
                         startSessionCallback);
     
    //Send the user to the main screen when they click on the notification.  
    //This should be replaced with a more meaningful location
    //designed specifically to handle notifications.
 
    Intent forGCM = new Intent (appContext, MainActivity.class);
 
    //Send a notification icon (for the tray) and large icon for the actual notification message.
    FuseAPI.setupGCM("<Sender ID>", forGCM, R.drawable.notification, R.drawable.Largeicon);
 }

Use the function FuseAPI.testGCMSetup(activity) to make sure everything is set up correctly. This function will output any errors to your console log.

NoteBubble.png

You must provide the small icon (Rdrawable.icon) for the GCM notifications. The large icon is optional but recommended.