安卓通知图标问题

标签 android push-notification firebase-cloud-messaging

我有一个奇怪的问题。我有两种方式在我的 Android 应用程序中发送通知;一个来自 Android 服务,另一个来自 FCM。

场景如下:

  1. 无论应用是否正在运行,Android 服务发送的通知图标都会正确显示。
  2. 当应用程序运行时,如果我通过 FCM 发送通知,通知图标仍会正确显示。
  3. 但如果该应用未运行并且我通过 FCM 发送通知,则会显示一个白色方 block 而不是通知图标。

我在 FCMService 中的代码:

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle("Android App")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());

最佳答案

您的问题很可能是 notification-messagesdata-messages 之间的区别。

请阅读:https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.

目前 FCM Web 控制台仅发送通知消息

因此,所有通过 Web 控制台(或通过带有 notification 负载的 API)发送的消息都将以这种方式发送:

  1. 如果应用已关闭或处于后台:FCM 将显示通知。如果你想定制它你可以,但你需要提供特定的配置(在 list 或发送 API 调用中)参见 https://firebase.google.com/docs/cloud-messaging/android/client#manifest
  2. 如果应用在前台:FCM 将调用 onMessageReceived()

.

如果您想要的行为是 onMessageReceived() 总是调用:
那么你需要使用一个data-only (no notification) message

关于安卓通知图标问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37616065/

相关文章:

android - GoogleApiAvailability 错过了 firebase-messaging :9. 4.0

Android 等效于 ActionBarActivity 中的 getActivity()

java - 在 android 布局中使用与分辨率无关的字体

windows - 更新通知中心 Azure WNS 时出错

ios - 应用程序 "Alarmy"能够从后台状态播放 iTunes 歌曲...如何?

javascript - 我找不到这个语法错误

Android FCM通知分组

java - 保存动态创建的按钮android

android - 安卓市场商户账号

android - 当应用程序在 Firebase 后台运行时如何处理通知