【发布时间】:2019-07-02 09:05:27
【问题描述】:
我正在尝试在收到 fcm 消息并且 APP 在后台时显示通知。我尝试了不同风格的通知,但没有运气。通知不会显示
这里是代码
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(GlobalVar.TAG, "From: " + remoteMessage.getFrom());
super.onMessageReceived(remoteMessage);
String channelId = getString(R.string.notification_channel_id);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
我知道这会被调用,因为我得到了日志。 如果应用程序在后台,则 FCM 会显示通知。当应用程序在前台时,上面的代码不显示它 有什么建议吗?
PS:我在Application类中创建了频道
我没有在日志猫中得到错误。我还按照下面的代码设置所有通知设置。请注意,我的设备在后台时已经接收并显示来自 FCM 的通知。但是当它在前台并且“我”处理通知的显示时,它就不起作用了
【问题讨论】:
-
提供您的清单
-
检查设置中是否允许该应用程序通知
-
这也取决于你如何创建 NotificationChannel。尝试在您的代码中使用
IMPORTANCE_HIGH创建 NotificationChannel。在某些设备中,如果您的通知通道的优先级不高,则不会显示通知。 -
在 FCM 的实施中。在您没有修改来自 FCM 的 JSON 之前,您无法在后台显示通知。您需要将 JSON 从通知标签修改为数据标签。
-
medium.com/@piyush052/…查看此链接。
标签: android firebase android-notifications firebase-notifications