Push Notifications Plugin for your Cocos2dx App

Datetime:2016-08-23 04:31:30          Topic: Cocos2d-X  Andrew Development           Share

It has always been a challenge to integrate push notifications into a project which is not written in a native language. Cocos2dx projects which are based on C++ also fall in this category and have to face the same challenge.  Since App42 already has Cocos2dx SDK written in C++, we thought we should go one step ahead and address the push notification issue by developing a plugin for the same.

We have come up with a plug-in to ease the process of integrating push notification for iOS and Android in Cocos2dx projects. The plug-in contains native files and libraries needed for configuring push as well as common cpp files that communicate with the native files in order to get device token or receiving push. We have also configured a Sample Project to demonstrate the integration of the plug-in.

This blog will walk you through the integration process step by step by taking the examples from the sample we have created. Before we start, first download the sample and plugin from here and unzip. The downloaded folder contains App42Cocos2dxSample and PushPlugin. The PushPlugin folder contains three folders:

  • iOS : contains native files needed for iOS
  • Android : contains native files and libs needed for Android
  • App42PushHelper : Common cpp files which will interact with native code as well as your project code

Integrating the Plugin:

We are assuming that you already have a running cocos2dx project to integrate our push plug-in. Steps given below -

Step-1 :Copy App42PushHelper folder from PushPlugin folder to the project folder parallel to classes folder

Step-2 :Let’s do some platform dependent settings:

    iOS Native:
    • Copy below mentioned files to folder proj.ios_mac->ios
    • App42Bridge(.h & .mm) 
      AppController+App42PushExt(.h & .mm)
    • Open your project in Xcode and add above files to the project under ios group
    • Drag and drop App42PushHelper folder from your project folder to xcode project
    Android:
    • Add path of common cpp files in your Android.mk file as follows:
    • Add "../../App42PushHelper/App42PushHelper.cpp" path in LOCAL_SRC_FILES
      Add "$(LOCAL_PATH)/../../App42PushHelper" path in LOCAL_C_INCLUDES
    • Copy plugin source from PushPlugin/Android folder to your src folder of Android project with same directory structure “app42/push/coco”
    • Copy .jar files from PushPlugin/Android/libs folder to your libs folder of proj.Android
    • Add following permission in your AndroidManifest.xml file(ignore this step if already added):
    • <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.VIBRATE" />
       <uses-permission android:name="android.permission.GET_ACCOUNTS" />
      <uses-permission android:name="android.permission.WAKE_LOCK" />
      <permission
      	android:name="com.shephertz.sample.permission.C2D_MESSAGE"
      	android:protectionLevel="signature" />
      <uses-permission android:name="com.shephertz.sample.permission.C2D_MESSAGE" />
      <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
      
    • Add Receiver component in your Androidmanifest.xml file:
    • <receiver
      	android:name="app42.push.coco.App42GCMReceiverr"
      	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="com.shephertz.sample" />
      	</intent-filter>
      </receiver>
      
    • Declare Service in your AndroidManifest.xml file:
    • <service android:name="app42.push.coco.App42GCMService" >
      </service>
    • Replace “com.shephertz.sample” with your application package name in AndroidManifest.xml file
    • Open App42PushHelper.cpp file from App42PushHelper folder & update the Google Project Id as follows:
    • #define GOOGLE_PROJECT_NUMBER "Your_Google_Project_Number" //Needed for android

Using the Plugin:Once you are done with platform dependent settings, now its time to use the plug-in. Using this plugin requires minimal coding, you only need to communicate with App42PushHelper class and rest will be taken care by the plug-in.

Step-1 :Include App42PushHelper.h to class where you want to use the plugin(In the sample we are using it in TestPushNotificationService class) as follows: 

#include "App42PushHelper.h"

Step-2 :Define two callback functions in the class one for getting the device token and other for getting push messages as follows:

/**
* Callback where you will get device token when device is registered successfully
*/
void TestPushNotificationService::deviceDidRegisteredWithDeviceToken(const char* deviceToken)
{
	log("deviceToken = %s",deviceToken);
}
/**
* Callback where you will receive push meassges
*/
void TestPushNotificationService::didReceivePushNotification(const char* pushMsg)
{
	log("Message = %s",pushMsg);
}

Step-3 :Set the callback function to receive your push messages already defined in step 2 as follows:

//Add this line in init function of the class you are working as we have added in the
//sample
App42PushHelper::setPushCallBack(CC_CALLBACK_1(TestPushNotificationService::didReceivePushNotification, this));

Step-4 :Register your device to APNS/GCM as follows:

App42PushHelper::registerForPush(CC_CALLBACK_1(TestPushNotificationService::deviceDidRegisteredWithDeviceToken, this));

Here we are calling “registerForPush” function of App42PushHelper class. The callback function “deviceDidRegisteredWithDeviceToken” will be called by the plug-in once the device is registered successfully to APNS. You will be getting a deviceToken in this function. The above statement can be called whenever you want to register your device for push, normally in the init method of the working class.

Congratulations, you have just equipped your app with Push Notification. But the job is not yet done as to send push you have to configure your app on App42 Cloud and store your device token which you got in step 4. To know more in details, do follow our tutorial Cocos2D-X Push Notification .

If you have queries or need further assistance, please feel free to reach us at support@shephertz.com





About List