gpt4 book ai didi

java - Firebase 云消息通知仅在我打开 "notification tab"时显示

转载 作者:行者123 更新时间:2023-12-02 02:03:08 25 4
gpt4 key购买 nike

因此,我通过 Google 的 Firebase Cloud Messaging 收到了推送通知。现在唯一的问题是通知不显示任何警报,只有当我拉下通知抽屉时我才看到它在那里。

我有这部分代码,我认为添加了“弹出”功能

public void displayNotification(String title, String body){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, Constants.CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body);

Intent intent = new Intent(mContext, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if(mNotificationManager != null) {
mNotificationManager.notify(1, mBuilder.build());
}

}

我遇到的另一个问题是,当我单击通知时,它会打开 Activity ,但不会删除通知。

最佳答案

前台弹出窗口

在构建器下,您需要设置高或最大优先级以及默认通知振动/声音,以便您可以看到“弹出”窗口

.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL);

后台弹出窗口

要实现后台弹出,您需要微调 FCM 负载。如果您的负载中同时包含 datanotification,则您的 displayNotification 方法无法处理弹出窗口。您将需要一个仅数据的有效负载。

Google 已将此行为放入文档中。 enter image description here引用-FCM for android: popup system notification when app is in background

自动取消

对于第二个问题,请在构建器中添加 setAutoCancel

.setAutoCancel(true)

额外说明

对于小米、红米等部分设备,您需要前往“设置”开启 float 通知

enter image description here

关于java - Firebase 云消息通知仅在我打开 "notification tab"时显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51262220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com