title: Getting twice GCM PUSH notification when application is closed or application is in background
body: <p>I have implemented gcm push notification in my application. Everything is working fine and i am getting notifications as well.</p>
  
  <p><strong>Problem:</strong></p>
  
  <ul>
  <li>When app is in background or terminated state i am getting 2 notifications at a time.</li>
  <li>When app is in foreground getting only 1 notification as i want.</li>
  </ul>
  
  <blockquote>
    <p>App should get only 1 notification as requirement but unfortunately facing undefined situation. </p>
  </blockquote>
  
  <p>My code below:</p>
  
  <p><code>GCMPushReceiverService</code> class for receiving message.</p>
  
  <pre><code>public class GCMPushReceiverService extends GcmListenerService {
  
  //This method will be called on every new message received
  @Override
  public void onMessageReceived(String from, Bundle data) {
      //Getting the message from the bundle
      String message = data.getString("message");
      //Displaying a notiffication with the message
      Log.e("MeSs",""+message);
      sendNotification(this,message, "Traccar App");
      sendNotification(message);
  
  }
  
  //This method is generating a notification and displaying the notification  //When in front
  private void sendNotification(Context context, String notificationText,
                                String notificationTitle) {
  
      PowerManager pm = (PowerManager) context
              .getSystemService(Context.POWER_SERVICE);
      PowerManager.WakeLock wakeLock = pm.newWakeLock(
              PowerManager.PARTIAL_WAKE_LOCK, "");
      wakeLock.acquire();
  
      Intent intent = new Intent(this, Home.class);
      intent.putExtra("ChatFragment", "newChatFound");
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      int requestCode = 0;
      PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
      Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
              context)
              .setSmallIcon(R.drawable.bug_log_two)
              .setColor(Color.RED)
              .setContentTitle(notificationTitle)
              .setContentText(notificationText)
              .setDefaults(Notification.DEFAULT_ALL)
              .setAutoCancel(true)
              .setContentIntent(pendingIntent);
  
  
      NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  
      notificationManager.notify(0, notificationBuilder.build()); //0 = ID of notification
  
      wakeLock.release();
  }
  
  private void sendNotification(String message) {
      Intent intent = new Intent(this, Home.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      int requestCode = 0;
      PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
      Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  
      NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
              .setSmallIcon(R.mipmap.ic_launcher)
              .setContentText(message)
              .setAutoCancel(true)
              .setContentIntent(pendingIntent);
  
      NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  
      notificationManager.notify(0, noBuilder.build()); //0 = ID of notification
  }
  
  }
  </code></pre>
  
  <p><code>manifest</code> file code:</p>
  
  <pre><code> &lt;!-- GCM --&gt;
  
      &lt;receiver
          android:name="com.google.android.gms.gcm.GcmReceiver"
          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.vk.trackeruser" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
      &lt;!-- GCM Receiver Service --&gt;
      &lt;service
          android:name=".Notification.GCMPushReceiverService"
          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;!-- GCM Registration Intent Service --&gt;
      &lt;service
          android:name=".Notification.GCMRegistrationIntentService"
          android:exported="false"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="com.google.android.gms.iid.InstanceID" /&gt;
          &lt;/intent-filter&gt;
      &lt;/service&gt;
  </code></pre>
  
  <p><code>GCMRegistrationIntentService</code> class:</p>
  
  <pre><code>public class GCMRegistrationIntentService extends IntentService {
  //Constants for success and errors
  public static final String REGISTRATION_SUCCESS = "RegistrationSuccess";
  public static final String REGISTRATION_ERROR = "RegistrationError";
  public static final String SenderId = "my id with numeric number ex 9897979";
  //Class constructor
  public GCMRegistrationIntentService() {
      super("");
  }
  
  
  @Override
  protected void onHandleIntent(Intent intent) {
      //Registering gcm to the device
      registerGCM();
  }
  
  private void registerGCM() {
      //Registration complete intent initially null
      Intent registrationComplete = null;
  
      //Register token is also null
      //we will get the token on successfull registration
      String token = null;
      try {
          //Creating an instanceid
          InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
  
          //Getting the token from the instance id
          token = instanceID.getToken(SenderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
  
          //Displaying the token in the log so that we can copy it to send push notification
          //You can also extend the app by storing the token in to your server
          Log.w("GCMRegIntentService", "token:" + token);
        String  tokan = token;
          //on registration complete creating intent with success
          registrationComplete = new Intent(REGISTRATION_SUCCESS);
  
          //Putting the token to the intent
          registrationComplete.putExtra("token", token);
      } catch (Exception e) {
          //If any error occurred
          Log.w("GCMRegIntentService", "Registration error");
          registrationComplete = new Intent(REGISTRATION_ERROR);
      }
      //Sending the broadcast that registration is completed
      LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
  }
      }
  </code></pre>
  
  <p><code>GCMTokenRefreshListenerService</code> class:</p>
  
  <pre><code>public class GCMTokenRefreshListenerService extends InstanceIDListenerService{
    //If the token is changed registering the device again
      @Override
      public void onTokenRefresh() {
          Intent intent = new Intent(this, GCMRegistrationIntentService.class);
          startService(intent);
      }
  }
  </code></pre>
  
  <p>Class for getting my <code>GCM</code> token:</p>
  
  <pre><code>   in oncreate {
      //Initializing our broadcast receiver
      mRegistrationBroadcastReceiver = new BroadcastReceiver() {
  
          //When the broadcast received
          //We are sending the broadcast from GCMRegistrationIntentService
  
          @Override
          public void onReceive(Context context, Intent intent) {
              //If the broadcast has received with success
              //that means device is registered successfully
              if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_SUCCESS)){
                  //Getting the registration token from the intent
                  String token = intent.getStringExtra("token");
                  StaticContents.Gcm_token=token;
                  Log.e("Token",""+token);
                  //Displaying the token as toast
                  Toast.makeText(getApplicationContext(), "Registration token:" + token, Toast.LENGTH_LONG).show();
  
                  //if the intent is not with success then displaying error messages
              } else if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_ERROR)){
                  //        Toast.makeText(getApplicationContext(), "GCM registration error!", Toast.LENGTH_LONG).show();
              } else {
                  //          Toast.makeText(getApplicationContext(), "Error occurred", Toast.LENGTH_LONG).show();
              }
          }
      };
  
      //Checking play service is available or not
      int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
  
      //if play service is not available
      if(ConnectionResult.SUCCESS != resultCode) {
          //If play service is supported but not installed
          if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
              //Displaying message that play service is not installed
              //        Toast.makeText(getApplicationContext(), "Google Play Service is not install/enabled in this device!", Toast.LENGTH_LONG).show();
              GooglePlayServicesUtil.showErrorNotification(resultCode, getApplicationContext());
  
              //If play service is not supported
              //Displaying an error message
          } else {
              //          Toast.makeText(getApplicationContext(), "This device does not support for Google Play Service!", Toast.LENGTH_LONG).show();
          }
  
          //If play service is available
      } else {
          //Starting intent to register device
          Intent itent = new Intent(this, GCMRegistrationIntentService.class);
          startService(itent);
      }
  }
   //Registering receiver on activity resume
  @Override
  protected void onResume() {
      super.onResume();
      Log.w("MainActivity", "onResume");
      LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
              new IntentFilter(GCMRegistrationIntentService.REGISTRATION_SUCCESS));
      LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
              new IntentFilter(GCMRegistrationIntentService.REGISTRATION_ERROR));
  }
  
      //Unregistering receiver on activity paused
  @Override
  protected void onPause() {
      super.onPause();
  
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
  
  }
  </code></pre>
  
  <blockquote>
    <p>Output 1: When the app in visible (foreground) only one notification is received. </p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/QVNIt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QVNIt.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p>Output 2: When app is closed or app is in background Getting 2 notifications.</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/wRrph.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wRrph.png" alt="enter image description here"></a></p>