title: How to send and receive notifications like social media apps
body: <p>I have successfully developed a chat app using java and firebase and I am stuck at sending and receiving chat notifications when chatting with a friend who has install my app. i have tried using FCM but to no avail. I have written the code and when i send a message, the friend doesn't receive a notification whether app is in foreground or background as well as no errors are found. please help me out. Below is the code for that class that extends the <code>FirebaseMessagingService</code>.</p>
  <pre><code>@SuppressLint(&quot;MissingFirebaseInstanceTokenRefresh&quot;)
  public class NotifyFirebaseMessaging extends FirebaseMessagingService {
  
      @Override
      public void onMessageReceived(@NonNull @NotNull RemoteMessage remoteMessage) {
          super.onMessageReceived(remoteMessage);
  
         
          String sent = remoteMessage.getData().get(&quot;sent&quot;);
          String user = remoteMessage.getData().get(&quot;user&quot;);
  
          SharedPreferences preferences = getSharedPreferences(&quot;PREFS&quot;, MODE_PRIVATE);
          String currentUser = preferences.getString(&quot;currentuser&quot;, &quot;none&quot;);
  
          FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
          if(firebaseUser != null &amp;&amp; sent.equals(firebaseUser.getUid())) {
              if (!currentUser.equals(user)) {
                  if(Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O){
                      sendOreoNotification(remoteMessage);
                  }
                  else{
                      sendNotification(remoteMessage);
  
                  }
              }
  
  
  
          }
      }
  
      @RequiresApi(api = Build.VERSION_CODES.O)
      private void sendOreoNotification(RemoteMessage remoteMessage){
          String user = remoteMessage.getData().get(&quot;user&quot;);
          String icon = remoteMessage.getData().get(&quot;icon&quot;);
          String title = remoteMessage.getData().get(&quot;title&quot;);
          String body = remoteMessage.getData().get(&quot;body&quot;);
  
          RemoteMessage.Notification notification = remoteMessage.getNotification();
          assert user != null;
          int j = Integer.parseInt(user.replaceAll(&quot;[\\D]&quot;, &quot;&quot;));
          Intent intent = new Intent(this, ChatActivity.class);
          Bundle bundle = new Bundle();
          bundle.putString(&quot;userid&quot;, user);
          intent.putExtras(bundle);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          PendingIntent pendingIntent = PendingIntent.getActivity(this, j, intent,PendingIntent.FLAG_ONE_SHOT);
          Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  
          OreoNotification oreoNotification = new OreoNotification(this);
          Notification.Builder builder = oreoNotification.getOreoNotification(title,body,pendingIntent,defaultSound,icon);
  
          int i = 0;
          if(j&gt;0){
              i = j;
          }
          oreoNotification.getManager().notify(i, builder.build());
      }
  
      private void sendNotification(RemoteMessage remoteMessage) {
          String user = remoteMessage.getData().get(&quot;user&quot;);
          String icon = remoteMessage.getData().get(&quot;icon&quot;);
          String title = remoteMessage.getData().get(&quot;title&quot;);
          String body = remoteMessage.getData().get(&quot;body&quot;);
  
          RemoteMessage.Notification notification = remoteMessage.getNotification();
          assert user != null;
          int j = Integer.parseInt(user.replaceAll(&quot;[\\D]&quot;, &quot;&quot;));
          Intent intent = new Intent(this, ChatActivity.class);
          Bundle bundle = new Bundle();
          bundle.putString(&quot;userid&quot;, user);
          intent.putExtras(bundle);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          PendingIntent pendingIntent = PendingIntent.getActivity(this, j, intent,PendingIntent.FLAG_ONE_SHOT);
          Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                  .setSmallIcon(Integer.parseInt(icon))
                  .setContentTitle(title)
                  .setContentText(body)
                  .setAutoCancel(true)
                  .setSound(defaultSound)
                  .setContentIntent(pendingIntent);
          NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  
          int i = 0;
          if(j&gt;0){
              i = j;
          }
          notificationManager.notify(i, builder.build());
      }
  }
  </code></pre>