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

标签 android firebase firebase-cloud-messaging firebase-notifications

这是我的代码

 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

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

最佳答案

official documentation 中所述如果应用程序在后台运行,通知只会自动添加到系统托盘。如果应用程序在前台运行,通知将改为发送到您服务的 onMessageReceived 方法。

因此总结一下基于应用状态的 Firebase 通知会做什么:

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

关于android - 应用程序运行时不显示 Firebase 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38940962/

相关文章:

android - 当应用程序被杀死时,FCM 不工作

android - 图像浏览在 5.1 android 设备中不起作用

android - 无法使用 Android Studio 3.0 + DataBinding + Kotlin 构建项目

java - android中retrofit2 api请求的响应主体始终为空

javascript - 最佳实践 : Nested subscriptions to receive user data?

javascript - 如何从Array中抓取某条数据?

android - 将 backstack 与 ViewPager 结合使用

javascript - 注销后清除 Firebase 持久性索引数据库

iOS FCM 通知从 Google 控制台工作,但不是从服务器工作

iOS FirebaseCloudMessaging 通知在调试/测试飞行或发布中不起作用