Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 312
Hüseyin YILMAZ Avatar asked Aug 04 '26 17:08

Hüseyin YILMAZ


2 Answers

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
like image 67
TR4Android Avatar answered Aug 07 '26 06:08

TR4Android


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.

like image 39
Kharda Avatar answered Aug 07 '26 08:08

Kharda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!