title: Gcm notification received after hours on some devices
body: <p>I am trying to use gcm notification. My server code is working fine and I am getting success as acknowledgment.</p>
  
  <p>The problem is notification is getting sent properly :  </p>
  
  <p>1) In most of devices notification is received instantaneously.
     Tested on google nexus,sony phones.</p>
  
  <p>2) Other devices are also receiving notification but after several hours. Yes, <strong>hours</strong>. Tested on Karbonn, Micromax some phones.</p>
  
  <p><em>Note:</em></p>
  
  <p>All the devices are connected to the same wifi so network connectivity not an issue . Using php at server side;
  There are several unanswered questions regarding this topic . I am hereby listing some of them:</p>
  
  <p><a href="https://stackoverflow.com/questions/29619831/gcm-notification-is-not-working-on-some-devices-like-micromax">gcm notification is not working on some devices like micromax</a></p>
  
  <p><a href="https://stackoverflow.com/questions/26132407/one-device-doesnt-receive-push-notifications-gcm">One device doesn&#39;t receive push notifications (GCM)</a></p>
  
  <p><a href="https://stackoverflow.com/questions/22741114/push-notifications-delay-with-gcm">Push notifications delay with GCM</a></p>
  
  <p>Anyone else having similar problem attach your question too.</p>
  
  <p><em>Failed trials of rectification:</em></p>
  
  <p>I have also made changes to code after going through several questions in which the developers found their solution like 
  removing this line of code from <code>onHandleIntent()</code></p>
  
  <pre><code>GcmBroadcastReceiver.completeWakefulIntent(intent);
  </code></pre>
  
  <p>Or changing <code>delay_while_ideal</code> value to <code>true/false</code> in server code.</p>
  
  <p>Or separately mentioning receiver and registration intent-filter </p>
  
  <pre><code>&lt;intent-filter&gt;
  &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
  &lt;category android:name="com.nothing.gcmclient" /&gt;
  &lt;/intent-filter&gt;
  &lt;intent-filter&gt;
  &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt;
  &lt;category android:name="com.nothing.gcmclient" /&gt;
  &lt;/intent-filter&gt;
  </code></pre>
  
  <p><em>Code:
  Android.manifest</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.nothing.gcmclient"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="9"
          android:targetSdkVersion="22" /&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  
      &lt;permission
          android:name="com.nothing.gcmclient.permission.C2D_MESSAGE"
          android:protectionLevel="signature" /&gt;
  
      &lt;uses-permission android:name="com.nothing.gcmclient.permission.C2D_MESSAGE" /&gt;
      &lt;uses-permission android:name="com.nothing.gcmclient.c2dm.permission.RECEIVE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
  
      &lt;uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" &gt;
          &lt;activity
              android:name=".RegisterActivity"
              android:label="@string/app_name" &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;activity
              android:name=".MainActivity"
              android:configChanges="orientation|keyboardHidden"
              android:label="@string/app_name" &gt;
          &lt;/activity&gt;
  
          &lt;receiver
              android:name=".GcmBroadcastReceiver"
              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.nothing.gcmclient" /&gt;
              &lt;/intent-filter&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt;
                  &lt;category android:name="com.nothing.gcmclient" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  
          &lt;service android:name=".GCMNotificationIntentService"&gt;&lt;/service&gt;
  
          &lt;activity
              android:name=".ChatActivity"
              android:label="@string/title_activity_chat" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".RegisterScreen"
              android:label="@string/title_activity_register_screen" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".RegisterChatButtonActivity"
              android:label="@string/title_activity_register_chat_button" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".ChatHistory"
              android:label="@string/title_activity_chat_history" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".MessageScreen"
              android:label="@string/title_activity_message_screen" &gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><em>Code: GCMNotificationIntentService.java</em></p>
  
  <pre><code>public class GCMNotificationIntentService extends IntentService {
  
      public static final int NOTIFICATION_ID = 1;
      private NotificationManager mNotificationManager;
      NotificationCompat.Builder builder;
  
      public GCMNotificationIntentService() {
          super("GcmIntentService");
      }
  
      public static final String TAG = "GCMNotificationIntentService";
  
      @Override
      protected void onHandleIntent(Intent intent) {
          Bundle extras = intent.getExtras();
          GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
  
          String messageType = gcm.getMessageType(intent);
  
          if (!extras.isEmpty()) {
              if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                      .equals(messageType)) {
                  sendNotification("Send error: " + extras.toString());
              } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                      .equals(messageType)) {
                  sendNotification("Deleted messages on server: "
                          + extras.toString());
              } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                      .equals(messageType)) {
                  String sender=extras.get(Config.SENDER).toString().toLowerCase();
                  String message=extras.get(Config.MESSAGE_KEY).toString();
  
                  if(!RegisterActivity.appVisible==true)
                  {
                      sendNotification("New message Received from "+ extras.get(Config.SENDER));
                  }
              }
          }
          //GcmBroadcastReceiver.completeWakefulIntent(intent);
      }
  
      private void sendNotification(String msg) {
          Log.d(TAG, "Preparing to send notification...: " + msg);
          mNotificationManager = (NotificationManager) this
                  .getSystemService(Context.NOTIFICATION_SERVICE);
  
          PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                  new Intent(this, ChatHistory.class), 0);
  
          NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                  this).setSmallIcon(R.drawable.gcm_cloud)
                  .setContentTitle("New Notification")
                  .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                  .setContentText(msg);
  
          mBuilder.setContentIntent(contentIntent);
          mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
          Log.d(TAG, "Notification sent successfully.");
      }
  }
  </code></pre>
  
  <p>Experts please look into the problem and suggest the appropriate cause and solution. My app would be of no use if it works only for certain phones.If you need any more file in our application kindly inform.</p>
  
  <p>PS- Kindly read the entire question and then post your comments or answers or mark duplicate if necessary.</p>