[Android Apps and Games] AdvaitT17:
In this post I will be explaining you guys that 'How to make a push notification app through GCM Service on PushBots'

Code is necessary for the implementation can be copied from here and watch the video below for the detail explanation of the implementation. Video will be uploaded soon

Android SDK integration

go to your module build.gradle file add PushBots dependency along with android support library v4:

Step 1: Import the Pushbots library into your project:

compile 'com.pushbots:pushbots-lib:2.0.13@aar'
compile 'com.android.support:support-v4:21.0.0'

Re-build your project and wait for gradle to finish.

Step 2: Update AndroidManifest.xml file:

Add the following permessions and change com.example.sampleapp with your app identifier.

<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission android:name="com.example.sampleapp.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.sampleapp.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive dataf message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

The following intent-filter in your main activity:

<intent-filter>
<action android:name="com.example.sampleapp.MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Add the following activity, reciever and service before the end of your application tag, and in all of the above code change com.example.sampleapp with your app identifier.

<receiver
android:name="com.pushbots.google.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- 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" />
<category android:name="com.example.sampleapp" />
</intent-filter>
</receiver>
<receiver android:name="com.pushbots.push.DefaultPushHandler" />
<service android:name="com.pushbots.push.GCMIntentService" />

Step 3: Creating pushbots.xml config file:

Go to Res folder -> Values --> Right click and new Resource File and name it pushbots with this content. Change your app ID and Sender ID.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Pushbots Application ID -->
<string name="pb_appid">548ef5901d0ab1</string>
<!-- GCM Sender ID -->
<string name="pb_senderid">48849973</string>
<!-- Pushbots Log Level log Tag "PB2" -->
<string name="pb_logLevel">DEBUG</string>
</resources>

Step 4: Add required code:

in your main Activity add this line:

Pushbots.sharedInstance().init(this);

That's pretty much it. Run your application now, if all goes well, your device should be registered and ready to recieve push notifications.

Step 5: Custom handler for notifications (Optional):

5.1. Create customHandler class.

public class customHandler extends BroadcastReceiver
{
private static final String TAG = "customHandler";
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
Log.d(TAG, "action=" + action);
// Handle Push Message when opened
if (action.equals(PBConstants.EVENT_MSG_OPEN)) {
//Check for Pushbots Instance
Pushbots pushInstance = Pushbots.sharedInstance();
if(!pushInstance.isInitialized()){
Log.d("Initializing Pushbots.");
Pushbots.sharedInstance().init(context.getApplicationContext());
}

//Clear Notification array
if(PBNotificationIntent.notificationsArray != null){
PBNotificationIntent.notificationsArray = null;
}

HashMap<?, ?> PushdataOpen = (HashMap<?, ?>) intent.getExtras().get(PBConstants.EVENT_MSG_OPEN);
Log.w(TAG, "User clicked notification with Message: " + PushdataOpen.get("message"));

//Report Opened Push Notification to Pushbots
if(Pushbots.sharedInstance().isAnalyticsEnabled()){
Pushbots.sharedInstance().reportPushOpened( (String) PushdataOpen.get("PUSHANALYTICS"));
}

//Start lanuch Activity
String packageName = context.getPackageName();
Intent resultIntent = new Intent(context.getPackageManager().getLaunchIntentForPackage(packageName));
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);

resultIntent.putExtras(intent.getBundleExtra("pushData"));
Pushbots.sharedInstance().startActivity(resultIntent);

// Handle Push Message when received
}else if(action.equals(PBConstants.EVENT_MSG_RECEIVE)){
HashMap<?, ?> PushdataOpen = (HashMap<?, ?>) intent.getExtras().get(PBConstants.EVENT_MSG_RECEIVE);
Log.w(TAG, "User Received notification with Message: " + PushdataOpen.get("message"));
}
}
}

5.2. Open AndroidManifest.xml and replace DefaultPushHandler with your custom Handler class.

<receiver android:name="PACKAGE_NAME.customHandler" />

5.3. Set your customHandler class in MainActivity.

Pushbots.sharedInstance().setCustomHandler(customHandler.class);

Pls hit the thanks button if i helped you :D:angel:

For more information-Visit: http://androidtechfreakat.blogspot.in

http://androidtechxdaat,weebly.com

http://advaitt17.github.io

XDA:DevDB Information

[GUIDE]To make a Push Notificaton App via GCM Service by AdvaitT17, App for all devices (see above for details)

Contributors

AdvaitT17, PushBots Developers

Source Code: www.pushbots.com

Version Information

Status: Stable

Created 2016-04-06

Last Updated 2016-05-20