Hüseyin YILMAZ
Hüseyin YILMAZ

Reputation: 157

Firebase Notification Does Not Show When Application Run

This is my Code

 private void showNotification(String message) {

    NotificationCompat.BigTextStyle stil = new NotificationCompat.BigTextStyle();
    stil.setBigContentTitle("Başıl");
    stil.bigText(message);
    Intent i=new Intent(this,Bilgi.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setAutoCancel(true);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setTicker("selam dost.mesaj geldi");
    builder.setDefaults(Notification.DEFAULT_ALL);
    builder.setContentIntent(pendingIntent);
    builder.setContentTitle("Bildirim");
    builder.setContentText(message);
    builder.setStyle(stil);
    NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0,builder.build());



}

enter image description here

When i send a notification (if application is working (left side of picture)) my notify does not show. when application working at the background my notify shows. How can i solve this problem ? thank you so much for your help.

Upvotes: 3

Views: 6531

Answers (2)

Kharda
Kharda

Reputation: 1368

On your onMessageReceived method, you have to get the notification message body and pass it to the showNotification(String message) method.

like this:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    String message = remoteMessage.getNotification().getBody();
    showNotification(message);
}

That way it will show the message you have set when sending the firebase notification.

Upvotes: 2

TR4Android
TR4Android

Reputation: 3246

As stated in the official documentation notifications are only added to the system tray automatically if the app runs in the background. If the app runs in the foreground the notification will instead be sent to the onMessageReceived method of your service.

So to summarize what will be done with Firebase Notifications based on app state:

  • Foreground: onMessageReceived will handle the notification
  • Background: Firebase will handle the notification and put it in the system tray for you

Upvotes: 7

Related Questions