Server:
var GCM = require('gcm').GCM;
var apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX';
var gcm = new GCM(apiKey);
var message = {
registration_id: 'APA91bEAjoFkJu6KF-UgbLWhB-qbHs6fHeYANpB1XFT4Y8NQbjaOJPQ_PItvBpPF5Zi3thEB6H-_0SXkT7JcYB4yGMOa-jZyeygkxTzy56bbqG8zqLSGouFpSr4F5uGvHzEywH_E3Lko8W57XiCe8F_NJGSvkA0i1jAvXkVvCYTzEyar-OAec10', // required
collapse_key: 'Collapse key',
'data.key1': 'value1',
'data.key2': 'value2'
};
gcm.send(message, function(err, messageId){
if (err) {
console.log("Something has gone wrong!");
} else {
console.log("Sent with message ID: ", messageId);
}
});
Client :
public class GcmMessageHandler extends IntentService {
String mes;
private Handler handler;
public GcmMessageHandler() {
super("GcmMessageHandler");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
mes = extras.getString("title");
showToast();
Log.i("GCM", "Received : (" +messageType+") "+extras.getString("title"));
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
public void showToast(){
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),mes , Toast.LENGTH_LONG).show();
}
});
}
}
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmMessageHandler will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
It worked yesterday, but now it does not show anything on a device. I have no idea what wrong is. I didn't change anything. Can someone have any idea about it? (I am working on a real device)
EDIT:
It works on emulator.
MANIFEST:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET" /> <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" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.hmkcode.android.gcm.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.hmkcode.android.gcm" /> </intent-filter> </receiver> <service android:name=".GcmMessageHandler" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application>
Check This code It may be help you :
//GCMHelper
}
//GCMIntentService
//Note Put this class in main package of your Application.
}
//Mainfest Config
*Note please change the package name According to your application main package.