title: Include GCM Push Notification into my app
body: <p>I want to send a notification to the user when a task is assigned (i.e on a button click). When a task is assigned to the user I change some values in my server(i.e i'm performing a post method) I would like to know how to achieve this.
  I have reached till registering the user when he logs in to my app &amp; i'm storing users token id into my server I have also gone through google developers guide and have registered my app and obtained senderid and serverapikey. I will post the code till where I have reached please help me on how to proceed further.</p>
  
  <p>LoginActivity</p>
  
  <pre><code> private void checkUserRegistrationToken() {
      String url = URLMap.getGcmtokenUrl("gcmtoken_url");
      employeeId = LoggedInUserStore.getLoggedInEmployeeId(getApplicationContext());
      companyId = LoggedInUserStore.getLoggedInCompanyId(getApplicationContext());
      url = url.replace("{eid}", employeeId).replace("{cid}", companyId);
      final StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener&lt;String&gt;() {
          @Override
          public void onResponse(String response) {
              try {
                      JSONObject jObj = new JSONObject(response);
                  tokenId = jObj.getString("TokenId");
                  if (tokenId.equals("null")) {
                      registerInBackground();
                  }
              } catch (JSONException e) {
  
              }
          }
      }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
              System.out.println("Error===" + error.toString());
          }
      });
      request.setRetryPolicy(new VolleyRetryPolicy().getRetryPolicy());
      RequestQueue queue = ((VolleyRequestQueue) getApplication()).getRequestQueue();
      queue.add(request);
  }
  
  private void registerInBackground() {
      new AsyncTask&lt;Void, Void, String&gt;() {
          @Override
          protected String doInBackground(Void... params) {
              String msg = "";
              try {
                  if (gcmObj == null) {
                      gcmObj = GoogleCloudMessaging.getInstance(LoginActivity.this);
                  }
                  tokenId = gcmObj.register(String.valueOf(R.string.gcm_defaultSenderId));
                  msg = "Registration ID:" + tokenId;
                  if (new ServiceManager(getApplicationContext()).isNetworkAvailable() &amp;&amp; checkPlayServices()) {
                      String storeUrl = URLMap.getGcmtokenPostUrl();
                      employeeId = LoggedInUserStore.getLoggedInEmployeeId(getApplicationContext());
                      companyId = LoggedInUserStore.getLoggedInCompanyId(getApplicationContext());
                      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
                      map.put("EmployeeId", employeeId);
                      map.put("CompanyId", companyId);
                      map.put("TokenId", tokenId);
                      JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, storeUrl, new JSONObject(map), new Response.Listener&lt;JSONObject&gt;() {
                          @Override
                          public void onResponse(JSONObject response) {
                              Log.i(TAG, "Token has been posted in server!");
                          }
                      }, new Response.ErrorListener() {
                          @Override
                          public void onErrorResponse(VolleyError error) {
                              Log.i(TAG, "Error posting token into server!!");
                          }
                      });
                      request.setRetryPolicy(new VolleyRetryPolicy().getRetryPolicy());
                      RequestQueue queue = ((VolleyRequestQueue) getApplication()).getRequestQueue();
                      queue.add(request);
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
              System.out.println("Token Mesage=" + msg);
              return msg;
          }
  
          @Override
          protected void onPostExecute(String s) {
          }
      }.execute();
  }
  </code></pre>
  
  <p>As you can see that I'm storing token id of user into my server.
  Now I have also created a GcmBroadcastReceiver class</p>
  
  <pre><code>public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
      ComponentName comp = new ComponentName(context.getPackageName(),
              NotificationService.class.getName());
      startWakefulService(context, (intent.setComponent(comp)));
      setResultCode(Activity.RESULT_OK);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and I have also created a NotificationService class which has on MessageReceived method</p>
  
  <pre><code>public class NotificationService extends GcmListenerService {
  
  
  public static final int notifyID = 9001;
  public static final String appname = "FM Ninja";
  NotificationCompat.Builder builder;
  
  public NotificationService() {
      //    super("GcmIntentService");
  }
  
  @Override
  public void onMessageReceived(String from, Bundle data) {
  
          Intent resultIntent = null;
          PendingIntent resultPendingIntent;
          resultIntent = new Intent(this, HomeActivity.class);
          resultPendingIntent = PendingIntent.getActivity(this, 0,
                  resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
  
          NotificationCompat.Builder mNotifyBuilder;
          NotificationManager mNotificationManager;
  
          mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  
          mNotifyBuilder = new NotificationCompat.Builder(this)
                  .setContentTitle("title")
                  .setTicker("New Message !")
                  .setContentText("first message please")
                  .setSmallIcon(R.mipmap.cms_launch_icon);
  
          // Set pending intent
          mNotifyBuilder.setContentIntent(resultPendingIntent);
  
          // Set Vibrate, Sound and Light
          int defaults = 0;
          defaults = defaults | Notification.DEFAULT_LIGHTS;
          defaults = defaults | Notification.DEFAULT_VIBRATE;
          defaults = defaults | Notification.DEFAULT_SOUND;
  
          mNotifyBuilder.setDefaults(defaults);
          mNotifyBuilder.setAutoCancel(true);
          // Post a notification
          mNotificationManager.notify(notifyID, mNotifyBuilder.build());
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The problem is even after adding permission in Manifeast when a call is made from server onMessageReceived is never getting executed Please help me.
  I will also post manifeast file</p>
  
  <pre><code> &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  
  &lt;!--GCM Permissions--&gt;
  
  &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt;
  &lt;uses-permission android:name="com.google.android.c2dm.permission.SEND"/&gt;
  &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
  &lt;permission android:name="com.six30labs.cms.permission.C2D_MESSAGE"
      android:protectionLevel="signature"/&gt;
  
  &lt;uses-permission android:name="com.six30labs.cms.permission.C2D_MESSAGE" /&gt;
  
  
  &lt;application
      android:name="com.six30labs.cms.general.VolleyRequestQueue"
      android:allowBackup="true"
      android:icon="@mipmap/cms_launch_icon"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
  
      &lt;activity android:name=".MainActivity"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
      &lt;activity android:name=".SplashScreen"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
  
      &lt;meta-data android:name="com.google.android.gms.version"
          android:value="@integer/google_play_services_version" /&gt;
  
      &lt;activity android:name=".LoginActivity" /&gt;
      &lt;activity android:name=".ForgotPassword" /&gt;
      &lt;activity android:name=".NoInternet" /&gt;
      &lt;activity android:name=".HomeActivity"/&gt;
      &lt;activity android:name=".ComplaintDetailsSupervisor" /&gt;
      &lt;activity android:name=".ComplaintDetailsEmployee" /&gt;
      &lt;activity android:name=".NavBarProfile" /&gt;
      &lt;activity android:name=".ComplaintDetailsWorker" /&gt;
      &lt;activity android:name=".AssignedDetailSupervisor" /&gt;
      &lt;activity android:name=".AcceptedComplaintDetailsWorker" /&gt;
      &lt;activity android:name=".VerifyDetailSupervisor"/&gt;
      &lt;activity android:name=".ManagerComplaintListActivity"/&gt;
  
      &lt;!--&lt;service android:name="com.six30labs.cms.storage.RegistrationIntentService"
          android:exported="false"/&gt;--&gt;
  
      &lt;receiver
          android:name="com.six30labs.cms.general.GcmBroadcastReceiver"
          android:exported="true"
          android:permission="com.google.android.c2dm.permission.SEND"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
              &lt;category android:name="com.six30labs.cms"/&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
      &lt;!-- Register Service --&gt;
  
      &lt;service android:name="com.six30labs.cms.general.NotificationService"
          android:exported="false" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
          &lt;/intent-filter&gt;
      &lt;/service&gt;
  
  &lt;/application&gt;
  </code></pre>
  
  <p>Server side script</p>
  
  <pre><code>public class GCMNotification
  {
      private CMSEntities db = new CMSEntities();
      static string gcmid = "AIzaSyB7HSIF1RIvkyCnpP6KtYiy6wQ-s6YBscY";
  
      public void AssignEmpNotification(string employeeid)
      {
          long id = Convert.ToInt64(employeeid);
          PushBroker pushBroker = new PushBroker();
          pushBroker.RegisterGcmService(new GcmPushChannelSettings(gcmid));
          pushBroker.QueueNotification(new GcmNotification().ForDeviceRegistrationId("APA91bGElkVodLyubuMM90TEnfUMab0Fs6JudsjXcgIUrTrT8Zk3GezKYWc9w2gGs6pzLLq_nPSZCXU30M5iYKdRJcKZnkafWuwhnihZQ88vcwUrKhiQn6eWSqGrLCeHFblVT09IR7jy")
             .WithJson(@"{""message"":""Hi Hello" + "wsfdasd" + @""",""title"":""title" + "vendorBids" + @""",""Bidsid"":""" + "1" + @""",""Eventdate"":""" + "2/2/2016" + @""",""vendorname"":""" + "name" + @"""}"));
  
          pushBroker.StopAllServices();
      }
  
  }
  </code></pre>