title: FCM wont make notification
body: <p>I have a chat app and I'm trying to display a notification to the user once a new message is received.</p>
  
  <p>Once I launch the activity I call <code>onStart</code> as follows:</p>
  
  <pre><code>@Override
  protected void onStart() {
      super.onStart();
      popNotification();
  }
  </code></pre>
  
  <p><code>popNotification</code> is a class that is called in order to check if there was any update in my database. If yes, I call another class called <code>AddNotification()</code></p>
  
  <pre><code>public void popNotification() {
  
      db.collection("Users").document(auth.getUid()).collection("MyChats")
              .addSnapshotListener(new EventListener&lt;QuerySnapshot&gt;() {
                  @Override
                  public void onEvent(@Nullable QuerySnapshot value,
                                      @Nullable FirebaseFirestoreException e) {
                      if (e != null) {
                          Log.w("", "Listen failed.", e);
                          return;
                      }
  
                      for (QueryDocumentSnapshot doc : value) {
  
                          if (doc.getId() != null) {
                              DocumentReference docRef = db.collection("Users").document(auth.getUid()).collection("MyChats").document(doc.getId());
                              docRef.get().addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
                                  @Override
                                  public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
                                      if (task.isSuccessful()) {
                                          DocumentSnapshot document = task.getResult();
                                          if (document.exists()) {
                                              if(document.getLong("LastMessageTime") &gt; document.getLong("lastChatVisited")){
                                                  AddNotification();
                                              }
                                          } else {
                                              Log.d("", "No such document");
                                          }
                                      } else {
                                          Log.d("", "get failed with ", task.getException());
                                      }
                                  }
                              });
                          }
                      }
                  }
              });
  }
  </code></pre>
  
  <p>and <code>AddNotification()</code>:</p>
  
  <pre><code>private void AddNotification(){
      FirebaseInstanceId.getInstance().getInstanceId()
              .addOnCompleteListener(new OnCompleteListener&lt;InstanceIdResult&gt;() {
                  @Override
                  public void onComplete(@NonNull Task&lt;InstanceIdResult&gt; task) {
                      if (!task.isSuccessful()) {
                          Log.w("", "getInstanceId failed", task.getException());
                          return;
                      }
  
                      // Get new Instance ID token
                      String token = task.getResult().getToken();
  
                      try {
  
                          String title = "TEST ";
                          MyTaskParams params = new MyTaskParams(token, title, "TTT!");
                          MyTask myTask = new MyTask();
                          myTask.execute(params);
  
                      } catch (Exception ex) {
                          ex.printStackTrace();
                      }
                  }
              });
  }
  </code></pre>
  
  <p>One last thing is the mytask which is:</p>
  
  <pre><code>private class MyTask extends AsyncTask&lt;MyTaskParams, Void, Void&gt; {
      @Override
      protected Void doInBackground(MyTaskParams... params) {
          String userDeviceIdKey = params[0].url;
          String title = params[0].title;
          String body = params[0].body;
  
          String authKey = "XXX";   // You FCM AUTH key
          String FMCurl = "https://fcm.googleapis.com/fcm/send";
          URL url;
          try {
              url = new URL(FMCurl);
              HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  
              conn.setUseCaches(false);
              conn.setDoInput(true);
              conn.setDoOutput(true);
  
              conn.setRequestMethod("POST");
              conn.setRequestProperty("Authorization","key="+authKey);
              conn.setRequestProperty("Content-Type","application/json");
  
              JSONObject json = new JSONObject();
              json.put("to",userDeviceIdKey);
              json.put("priority","high");
              JSONObject info = new JSONObject();
              info.put("title", title);   // Notification title
              info.put("body", body); // Notification body
              json.put("data", info);
  
              OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
              wr.write(json.toString());
              wr.flush();
              conn.getInputStream();
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
          return null;
      }
  }
  </code></pre>
  
  <p>Now, the problem is that I don't receive any notification.</p>
  
  <p>I inserted Logs in order to check if the code runs there and it seems to pass all the required functions however still it doesn't show anything.</p>
  
  <p>I added priority = high because I read it might affect but it didn't help.</p>
  
  <p>My message class is:</p>
  
  <pre><code>public class MyFirebaseMessagingService extends FirebaseMessagingService {
      private static final String TAG = "FCM Service";
  
  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
  
      Log.d(TAG, "From: " + remoteMessage.getFrom());
      Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().get("body"));
  
      NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "29358305")
              .setSmallIcon(R.drawable.ic_launcher_custom_background)
              .setContentTitle(remoteMessage.getData().get("title"))
              .setContentText(remoteMessage.getData().get("body"))
              .setStyle(new NotificationCompat.BigTextStyle()
                      .bigText(remoteMessage.getData().get("body")))
              .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(),
                      R.mipmap.ic_launcher))
              .setPriority(NotificationCompat.PRIORITY_DEFAULT);
  
      //createNotificationChannel();
  
      NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
  
      notificationManager.notify(235345305, builder.build());
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>EDIT:</p>
  
  <p>I also tried to use the following when creating the notification without any improvement:</p>
  
  <pre><code>public class MyFirebaseMessagingService extends FirebaseMessagingService {
      private static final String TAG = "FCM Service";
  
      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
  
          Log.d(TAG, "From: " + remoteMessage.getFrom());
          Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().get("body"));
  
  
          createNotificationChannel();
          notifyThis(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
  
      }
  
  private void createNotificationChannel() {
      // Create the NotificationChannel, but only on API 26+ because
      // the NotificationChannel class is new and not in the support library
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
          CharSequence name = "ABC";
          String description = "ABCDE";
          int importance = NotificationManager.IMPORTANCE_DEFAULT;
          NotificationChannel channel = new NotificationChannel("191919", name, importance);
          channel.setDescription(description);
          // Register the channel with the system; you can't change the importance
          // or other notification behaviors after this
          NotificationManager notificationManager = getSystemService(NotificationManager.class);
          notificationManager.createNotificationChannel(channel);
      }
  }
  
  public void notifyThis(String title, String message) {
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "191919")
              .setSmallIcon(R.drawable.ic_launcher_background)
              .setContentTitle(title)
              .setContentText(message)
              .setPriority(NotificationCompat.PRIORITY_DEFAULT);
  
      NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
  
      // notificationId is a unique int for each notification that you must define
      notificationManager.notify(0, mBuilder.build());
  }
  </code></pre>
  
  <p>Do I also need to add <code>onPause</code> or something else for it to work or do I have some other bug that I cant understand?</p>
  
  <p>Thank you</p>