Over a million developers have joined DZone.

Push Notifications Plugin for Your Cocos2dx App

DZone's Guide to

Push Notifications Plugin for Your Cocos2dx App

Learn how an App42 Cloud plugin can solve the problem of integrating push notifications into Cocos2dx projects, which are based on C++.

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

It has been always 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 have to face the same challenge. We have already equipped App42 Cloud users with Cocos2dx SDK written in C++, so we thought to go one step ahead and solve this problem, too.

We have come up with a plugin 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 notifications as well as common cpp files that communicate with the native files in order to get device token or receiving push notifications. We have also configured a sample project to demonstrate the integration of the plugin.

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

  • iOS: files needed for iOS.
  • Android: 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

Step 1: Copy the App42PushHelper folder from the PushPlugin folder to the project folder parallel to the classes folder.
Step 2: Let’s do some platform dependent settings:


Copy the below files to folder proj.ios_mac->ios

App42Bridge(.h & .mm)

AppController+App42PushExt(.h & .mm)

  • Open your project in Xcode and add the above files to the project under the iOS group.
  • Drag and drop the App42PushHelper folder from your project folder to the Xcode project.


Add the path of common cpp files in your Android.mk file as follows:

Add the  "../../App42PushHelper/App42PushHelper.cpp"  path in LOCAL_SRC_FILES.

Add the  "$(LOCAL_PATH)/../../App42PushHelper"  path in LOCAL_C_INCLUDES.

  • Copy the plugin source from the PushPlugin/Android folder to your src folder of the Android project with same directory structure, “app42/push/coco.”
  • Copy the .jar files from PushPlugin/Android/libs folder to your libs folder of proj.Android.
  • Add the 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" /›

‹!-- GCM requires a Google account. --›

‹uses-permission android:name="android.permission.GET_ACCOUNTS" /›

‹!-- Keeps the processor from sleeping when a message is received. --›

‹uses-permission android:name="android.permission.WAKE_LOCK" /›


Creates a custom permission so only this app can receive its messages.

NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,

where PACKAGE is the application's package name.




android:protectionLevel="signature" /›

‹uses-permission android:name="com.shephertz.sample.permission.C2D_MESSAGE" /›

‹!-- This app has permission to register and receive data message. --›

‹uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /›
  • Add the Receiver component in your Androidmanifest.xml file:


android:permission="com.google.android.c2dm.permission.SEND" ›


‹!-- Receives the actual messages. --›

‹action android:name="com.google.android.c2dm.intent.RECEIVE" /›

‹!-- Receives the registration id. --›

‹action android:name="com.google.android.c2dm.intent.REGISTRATION" /›

‹!-- Your package name here --›

‹category android:name="com.shephertz.sample" /›


  • Declare the Service in your AndroidManifest.xml file:
‹service android:name="app42.push.coco.App42GCMService" ›

  • Replace “app42.push.coco.App42GCMService” with your application package name in AndroidManifest.xml file.
  • Using the plug-in: once you are done with platform dependent settings, now its time to use the plug-in.

Step 1: Include App42PushHelper.h in the 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 the 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

//Add this line in init function of the class you are working as we have added in the
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 the “registerForPush” function of the 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 notifications, normally in the init method of the working class.

Congratulations, you have just equipped your app with push notifications. But the job is not yet done, as to send push notifications, you have to configure your app on the App42 Cloud and store your device token. To know more details, do follow our tutorial Cocos2DX Push Notifications.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

cocos2d-x ,game development ,mobile app development ,mobile ,notifications

Published at DZone with permission of Rajeev Ranjan. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}