title: Firebase push notification sent twice
body: <p>I'm working on an app that has firebase notifications, i followed through the firebase tutorial guides in order to how to send and receive notifications but I'm receiving 2 notifications on other devices instead of receiving 1 and receiving a notification from the sent device although that device shouldn't receive the notification that was sent from it</p>
  
  <p>I saw other questions regarding uploading notifications twice but it didn't work for me</p>
  
  <p>P.S: the notification is sent from a custom dialog that has title edit text and body edit text</p>
  
  <p>here is the manifest:</p>
  
  <pre><code>&lt;service
          android:name=".MyFirebaseInstanceIDService"
          android:exported="false"&gt;
          &lt;intent-filter&gt;
              &lt;action
                  android:name="com.google.firebase.INSTANCE_ID_EVENT"/&gt;
          &lt;/intent-filter&gt;
      &lt;/service&gt;
  
  &lt;service
          android:name=".MyFirebaseMessagingService"
          android:exported="false"&gt;
          &lt;intent-filter&gt;
              &lt;action
                  android:name="com.google.firebase.MESSAGING_EVENT" /&gt;
          &lt;/intent-filter&gt;
      &lt;/service&gt;
  </code></pre>
  
  <p>I'm compiling these in the gradle:</p>
  
  <pre><code>compile 'com.google.firebase:firebase-core:10.0.0'
  compile 'com.google.firebase:firebase-messaging:10.0.0'
  </code></pre>
  
  <p>Here's the code for the custom dialog:</p>
  
  <pre><code>builder = new AlertDialog.Builder(this);
          final View view = View.inflate(this, R.layout.layout_send_notification, null);
          builder.setView(view)
                  .setCancelable(true)
                  .setPositiveButton(getString(R.string.send), new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          EditText editMessage = (EditText) view.findViewById(R.id.notification_message);
                          EditText editTitle = (EditText) view.findViewById(R.id.notification_title);
                          Calendar now = Calendar.getInstance();
                          int day = now.get(Calendar.DAY_OF_MONTH);
                          int month = now.get(Calendar.MONTH) + 1;
                          int year = now.get(Calendar.YEAR);
                          String message = editMessage.getText().toString().replace(' ', '_');
                          String title = editTitle.getText().toString().replace(' ', '_');
  
                          boolean cancel = false;
                          View focusView = null;
  
                          if (TextUtils.isEmpty(message)) {
                              editMessage.setError(getString(R.string.error_field_required));
                              focusView = editMessage;
                              cancel = true;
                          }
  
                          if (TextUtils.isEmpty(title)) {
                              editTitle.setError(getString(R.string.error_field_required));
                              focusView = editTitle;
                              cancel = true;
                          }
  
                          if (cancel)
                              focusView.requestFocus();
                          else {
                              RequestQueue queue = Volley.newRequestQueue(NotificationsActivity.this);
                              String url = "https://mysite.here/send_push.php?" +
                                      "message=" + message +
                                      "&amp;title=" + title +
                                      "&amp;day=" + day +
                                      "&amp;month=" + month +
                                      "&amp;year=" + year;
                              StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener&lt;String&gt;() {
                                  @Override
                                  public void onResponse(String response) {
                                      Log.d("send push response", response);
                                  }
                              }, new Response.ErrorListener() {
                                  @Override
                                  public void onErrorResponse(VolleyError error) {
                                      error.printStackTrace();
                                  }
                              });
                              queue.add(request);
                          }
                      }
                  })
                  .setNegativeButton(getString(R.string.button_cancel), new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          dialogInterface.cancel();
                      }
                  });
          sendNotificationDialog = builder.create();
  </code></pre>
  
  <p>here is the firebaseMessagingService:</p>
  
  <pre><code>@Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
      sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
  }
  
  private void sendNotification(String messageTitle, String messageBody) {
      Intent intent = new Intent(this, SplashActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
              PendingIntent.FLAG_ONE_SHOT);
  
      Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
              .setContentTitle(messageTitle)
              .setContentText(messageBody)
              .setSmallIcon(R.drawable.ic_stat_spe_bau)
              .setAutoCancel(true)
              .setSound(defaultSoundUri)
              .setContentIntent(pendingIntent);
  
      NotificationManager notificationManager =
              (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  
      notificationManager.notify(0 , notificationBuilder.build());
  }
  </code></pre>
  
  <p>here is the firebaseInstanceIDService:</p>
  
  <pre><code>@Override
  public void onTokenRefresh() {
      String refreshedToken = FirebaseInstanceId.getInstance().getToken();
      Log.d("firebase ID", refreshedToken);
      sendRegistrationToServer(refreshedToken);
  }
  
  private void sendRegistrationToServer(String token) {
      RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
      String url = "https://mysite.here/insertFCM_ID.php?fcmID=" + token;
      StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener&lt;String&gt;() {
          @Override
          public void onResponse(String response) {
              Log.d("fcm response", response);
          }
      }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
              error.printStackTrace();
          }
      });
      queue.add(request);
  }
  </code></pre>
  
  <p>send_push.php code:</p>
  
  <pre><code>&lt;?php
  header("Content-type: application/json, charset=UTF-8");
  require('db.php');
  
  $db = mysqli_connect($host, $username, $password) or die('Could not connect');
  mysqli_select_db($db, $db_name) or die('');
  $message    = $_GET['message'];
  $title      = $_GET['title'];
  $day        = $_GET['day'];
  $month      = $_GET['month'];
  $year       = $_GET['year'];
  
  $server_key = "somekey";
  $sql        = 'select fcm_token from fcm_info';
  $result     = mysqli_query($db, $sql) or print("Error");
  $key        = array();
  
  while ($row = mysqli_fetch_assoc($result)) {
      $key[] = $row;
  }
  
  $headers = array(
      'Authorization: key=' . $server_key,
      'Content-Type: application/json'
  );
  $single  = "";
  
  foreach($key as $value) {
      $single  = $value['fcm_token'];
  
      $fields  = array(
          'to' =&gt; $single,
          'notification' =&gt; array(
              'title' =&gt; str_replace("_", " ", $title),
              'body' =&gt; str_replace("_", " ", $message),
              'vibrate' =&gt; 1,
              'sound' =&gt; 1
          )
      );
      $payload = json_encode($fields);
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
      $result = curl_exec($ch);
  }
  
  mysqli_query($db, "INSERT INTO notifications (title, body, day, month, year) VALUES ('$title', '$message', '$day', '$month', '$year')");
  
  mysqli_close($db);
  echo $result;
  ?&gt;
  </code></pre>
  
  <p>i tried a lot of ways in order to find where the problem is but i couldn't know what it is</p>
  
  <p>if more information is needed i will re-edit this to add those required information</p>