Server:
var GCM = require(\'gcm\').GCM;
var apiKey = \'XXXXXXXXXXXXXXXXXXXXXXXXXXXX\';
var gcm = new GCM(apiKey);
var message = {
registration_id: \'AP
Check This code It may be help you :
protected void gcm() {
// TODO Auto-generated method stub
GCMHelper gcmhelper = new GCMHelper();
String gcmtokenAlready = gcmhelper.alreadyregisterGCM();
if(gcmtokenAlready.length()>0){
To do code here
}else{
gcmhelper.registerGCM();
}
//GCMHelper
public class GCMHelper {
private final String SENDER_ID = ContextProvider.getContext().getResources().getString(R.string.GCM_ID);
private final Context context = ContextProvider.getContext();
/**
* This method is used to register the device with the GCM.
*
* @param context the context
*/
public void registerGCM() {
GCMRegistrar.checkDevice(context);
GCMRegistrar.checkManifest(context);
GCMRegistrar.register(context, SENDER_ID);
}
/**
* Already register gcm.
*
* @param context the context
* @return the string
*/
public String alreadyregisterGCM() {
GCMRegistrar.checkDevice(context);
GCMRegistrar.checkManifest(context);
return GCMRegistrar.getRegistrationId(context);
}
}
//GCMIntentService
//Note Put this class in main package of your Application.
public class GCMIntentService extends GCMBaseIntentService {
/**
* Instantiates a new GCM intent service.
*/
public GCMIntentService() {
super(ContextProvider.getContext().getResources().getString(R.string.GCM_ID));
}
/*
* (non-Javadoc)
*
* @see
* com.google.android.gcm.GCMBaseIntentService#onError(android.content.Context
* , java.lang.String)
*/
@Override
protected void onError(Context arg0, String arg1) {
// TODO Auto-generated method stub
Log.d(TAG, "Error: " + "sError");
}
/*
* @see Used when message comes form GCM server. Call your notification
* class here
*/
/* (non-Javadoc)
* @see com.google.android.gcm.GCMBaseIntentService#onMessage(android.content.Context, android.content.Intent)
*/
@Override
protected void onMessage(Context context, Intent arg1) {
// TODO Auto-generated method stub
String notiData = arg1.getStringExtra("message");
/**Start BroadCast*/
Intent mintent = new Intent("PAYMENT_STATUS_MESSAGE");
mintent.putExtra("PAYMENT_STATUS_MESSAGE", notiData);
//Send BroadCast
sendBroadcast(mintent);
//Generate Notification
generateNotification(context, notiData);
}
/*
* (non-Javadoc)
*
* @see
* com.google.android.gcm.GCMBaseIntentService#onRegistered(android.content
* .Context, java.lang.String)
*/
@Override
protected void onRegistered(Context context, String token) {
/** Saving the device token in the Application Class */
Log.v(TAG, "Registered First Time");
}
/* (non-Javadoc)
* @see com.google.android.gcm.GCMBaseIntentService#onUnregistered(android.content.Context, java.lang.String)
*/
@Override
protected void onUnregistered(Context arg0, String arg1) { }
/**
* Issues a notification to inform the user that server has sent a message.
*
* @param context the context
* @param message the message
*/
private void generateNotification(Context context, String message) {
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, message, when);
String title = "New POC Alert";
Intent notificationIntent = new Intent(context, null);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
//Mainfest Config
<!-- GCM Permission -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission
android:name="com.sc.candidate.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.sc.candidate.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="vibrate" />
<!-- For GCM -->
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
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.sc.candidate" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
*Note please change the package name According to your application main package.