繁体   English   中英

应用程序运行时不显示Firebase通知

[英]Firebase Notification Does Not Show When Application Run

这是我的代码

 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());



}

在此输入图像描述

当我发送通知时(如果应用程序正在工作(图片的左侧)),我的通知不会显示。 当应用程序在后台工作时,我的通知显示。 我怎么解决这个问题 ? 非常感谢你的帮助。

官方文档中所述,如果应用程序在后台运行,通知仅会自动添加到系统托盘中。 如果应用程序在前台运行,则通知将发送到服务的onMessageReceived方法。

因此,总结一下基于应用状态对Firebase通知的处理方式:

  • 前台onMessageReceived将处理通知
  • 背景 :Firebase将处理通知并将其放入系统托盘中

onMessageReceived方法上,您必须获取通知消息正文并将其传递给showNotification(String message)方法。

像这样:

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

这样它将显示您在发送firebase通知时设置的消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM