// PushNotification and all classes // GcmBroadcastReceiver public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName()); // Start the service, keeping the device awake while it is launching. startWakefulService(context, (intent.setComponent(comp))); setResultCode(Activity.RESULT_OK); } } // GCMIntentService public class GCMIntentService extends IntentService { public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; NotificationCompat.Builder builder; public static String mMessage = null; public GCMIntentService() { super("GcmIntentService"); } public static final String TAG = "MyAppGCM"; @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); GoogleCloudMessaging mGCM = GoogleCloudMessaging.getInstance(this); String messageType = mGCM.getMessageType(intent); if (!extras.isEmpty()) { // has effect of unparcelling Bundle if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { sendNotification(extras); } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { sendNotification(extras); } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { sendNotification(extras); Log.i(TAG, "Received: " + extras.toString()); } } GcmBroadcastReceiver.completeWakefulIntent(intent); } private void sendNotification(Bundle msg) { mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); if (msg.getString("message") != null && !msg.getString("message").equals("")) { mMessage = msg.getString("message"); Intent notificationIntent = new Intent(this, MyFirstActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("" + getResources().getString(R.string.app_name)) .setStyle(new NotificationCompat.BigTextStyle().bigText(mMessage)).setContentText(mMessage) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); mBuilder.setContentIntent(contentIntent); mBuilder.setAutoCancel(true); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); } } } // MyFirstActivity with whole process of GCM public class MyFirstActivity extends FragmentActivity { private static final int GCM_REQUEST = 9000; String SENDER_ID = "XXXXXXXXXXXX"; // PROJECT ID OF GCM static final String TAG = "MyAppGCM"; GoogleCloudMessaging mGCM; Context context; String strDeviceToken; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_myfirst); context = getApplicationContext(); if (checkPlayServices()) { mGCM = GoogleCloudMessaging.getInstance(this); strDeviceToken = getRegistrationId(context); if (strDeviceToken.isEmpty()) { registerInBackground(); } } else { Log.i(TAG, "No valid Google Play Services found."); } } private String getRegistrationId(Context context) { String registrationId = SharedPreferencesManager.getPreference(SharedPreferencesManager.GCM_ID, "", MyFirstActivity.this); if (registrationId.isEmpty()) { Log.i(TAG, "Registration not found."); return ""; } return registrationId; } private boolean checkPlayServices() { int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context); if (resultCode != ConnectionResult.SUCCESS) { if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { GooglePlayServicesUtil.getErrorDialog(resultCode, this, GCM_REQUEST).show(); } else { Log.i(TAG, "This device is not supported."); finish(); } return false; } return true; } private void registerInBackground() { new AsyncTask() { @Override protected String doInBackground(Void... params) { String msg = ""; try { if (mGCM == null) { mGCM = GoogleCloudMessaging.getInstance(context); } strDeviceToken = mGCM.register(SENDER_ID); msg = "Device registered, registration ID=" + strDeviceToken; } catch (IOException ex) { msg = "Error :" + ex.getMessage(); } return msg; } @Override protected void onPostExecute(String msg) { storeRegistrationId(context, strDeviceToken); // Call WebAPIHelper for Put Device Entry with device token in Web-Database } }.execute(null, null, null); } private void storeRegistrationId(Context context, String strDeviceToken) { SharedPreferencesManager.setPreference(SharedPreferencesManager.GCM_ID, strDeviceToken, MyFirstActivity.this); } } /// In AndroidManifest.xml file