title: gcm sends me a push only when i am on wifi and cell network
body: <p>I am trying to make a simple messaging app for a class project when I send a push for my server to gcm when the phone is on  WiFi it works fine when I use 3g (4g not tested) network it doesn't send the push at all </p>
  
  <p>I tried on the post to gcm </p>
  
  <pre><code>$fields = array(
          'registration_ids'  =&gt; array( $reg_id ),
          'data'              =&gt; array( "message" =&gt; $message,"chat_id" =&gt; $chat_id ),
          'delay_while_idle'  =&gt; false,
          'time_to_live'      =&gt; 2019200
  
      ); 
  
  'delay_while_idle'  =&gt; true, and no successes
  
  
  &lt;permission
  android:name="in.ultraneo.c2dm.permission.C2D_MESSAGE"   
  android:protectionLevel="signature" /&gt;
  &lt;uses-permission android:name="in.ultraneo.c2dm.permission.C2D_MESSAGE" /&gt;
  &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
  &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
  &lt;uses-permission android:name="android.permission.GET_TASKS" /&gt;
  
  
  
  &lt;receiver
              android:name=".MyC2dmReceiver"
              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="in.ultraneo.c2dm" /&gt;
              &lt;/intent-filter&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt;
  
                  &lt;category android:name="in.ultraneo.c2dm" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  </code></pre>
  
  <p><strong>receiver class</strong></p>
  
  <pre><code> public class MyC2dmReceiver extends BroadcastReceiver {
  
      private static String KEY = "c2dmPref";
      private static String REGISTRATION_KEY = "registrationKey";
      private Context context;
      private String name,message,time,chat_id;
      private NotificationCompat.Builder notification;
      private NotificationManager manager;
      private  Bundle extras;
  
  
      @Override
      public void onReceive(Context context, Intent intent) {
          this.context = context;
          if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
              String registrationId = intent.getStringExtra("registration_id");
              handleRegistration(context, intent);
  
              if(registrationId!=null)
              {
                  Intent i=new Intent(context,register.class);
                  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  i.putExtra("regkey",registrationId);
                  context.startActivity(i);
              }
  
          } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
  
              Bundle bundle=intent.getExtras();
              if(bundle!=null)
              {
                  name = intent.getStringExtra("message");
                  message = intent.getStringExtra("message");
                  chat_id = intent.getStringExtra("chat_id");
                  //time= get time();
              }
              SharedPreferences sp = context.getSharedPreferences("OURINFO",context.MODE_PRIVATE);
              String active = sp.getString("chaton","");
  
               boolean flag = isForeground(context,"in.ultraneo.c2dm");
  
                          if(flag)
                          {
                              if(active.compareTo("yes")==0) {
                                  Message.Message(context, "active is true");
                                  Intent i = new Intent("ONLINE");
                                  i.putExtra("msg",message);
                                  i.putExtra("chat_id_rec",chat_id);
                                  i.putExtra("name",message);
                                  context.sendOrderedBroadcast(i,null);
                              }
                              else
                                  Message.Message(context,"active is false but onpause or other activity");
                          }
                           else
                          {
                              Message.Message(context,"app is off line");
                              sendNotification(message, intent.getStringExtra("message"), name);
                          }
          }
  
      }
  
  
      private void handleRegistration(Context context, Intent intent) {
          String registration = intent.getStringExtra("registration_id");
          if (intent.getStringExtra("error") != null) {
  
              Log.d("c2dm", "registration failed");
              String error = intent.getStringExtra("error");
              if(error == "SERVICE_NOT_AVAILABLE"){
                  Log.d("c2dm", "SERVICE_NOT_AVAILABLE");
              }else if(error == "ACCOUNT_MISSING"){
                  Log.d("c2dm", "ACCOUNT_MISSING");
              }else if(error == "AUTHENTICATION_FAILED"){
                  Log.d("c2dm", "AUTHENTICATION_FAILED");
              }else if(error == "TOO_MANY_REGISTRATIONS"){
                  Log.d("c2dm", "TOO_MANY_REGISTRATIONS");
              }else if(error == "INVALID_SENDER"){
                  Log.d("c2dm", "INVALID_SENDER");
              }else if(error == "PHONE_REGISTRATION_ERROR"){
                  Log.d("c2dm", "PHONE_REGISTRATION_ERROR");
              }
          } else if (intent.getStringExtra("unregistered") != null) {
  
              Log.d("c2dm", "unregistered");
              Toast.makeText(context,"C2DM unregistered sucessfully",Toast.LENGTH_SHORT).show();
  
          } else if (registration != null) {
  
              Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
              editor.putString(REGISTRATION_KEY, registration);
              editor.commit();
              Toast.makeText(context,"Registration Id:"+registration,Toast.LENGTH_SHORT).show();
              new UltraFileaccess().Load_Data("REGKEY", registration, context);
  
          }
      }
  
  
      public boolean isForeground(Context context,String PackageName){
          // Get the Activity Manager
          ActivityManager manager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
  
          // Get a list of running tasks, we are only interested in the last one,
          // the top most so we give a 1 as parameter so we only get the topmost.
          List&lt; ActivityManager.RunningTaskInfo &gt; task = manager.getRunningTasks(1);
  
          // Get the info we need for comparison.
          ComponentName componentInfo = task.get(0).topActivity;
  
          // Check if it matches our package name.
          if(componentInfo.getPackageName().equals(PackageName)) return true;
  
          // If not then our app is not on the foreground.
          return false;
      }
  
      private void sendNotification(String msg,String mobno,String name) {
  
          Bundle args = new Bundle();
          args.putString("mobno", mobno);
          args.putString("name", name);
          args.putString("msg", msg);
          Intent chat = new Intent(context, chatmsgbox.class);
          chat.putExtra("load",false);
          chat.putExtra("INFO", args);
          chat.putExtra("chat_id_notifi",chat_id);
          notification = new NotificationCompat.Builder(context);
          notification.setContentTitle(name);
          notification.setContentText(msg);
          notification.setDefaults(Notification.DEFAULT_ALL);
          notification.setTicker("New Message!");
          notification.setSmallIcon(R.drawable.icon);
  
          PendingIntent contentIntent = PendingIntent.getActivity(context,1000,chat, PendingIntent.FLAG_CANCEL_CURRENT);
          notification.setContentIntent(contentIntent);
          notification.setAutoCancel(true);
          manager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
          manager.notify(0, notification.build());
      }
  
  }
  </code></pre>