【问题标题】:Firebase Cloud Messaging Notification only shows when I open the "notification tab"Firebase 云消息通知仅在我打开“通知选项卡”时显示
【发布时间】:2018-07-10 09:54:42
【问题描述】:

所以我收到了使用 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());
        }

    }

我遇到的其他问题是,当我单击通知时,它会打开活动,但不会删除通知。

【问题讨论】:

    标签: java android push-notification notifications


    【解决方案1】:

    前台弹出窗口

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

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

    背景弹窗

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

    Google 已将此行为置于文档中。 参考——FCM for android: popup system notification when app is in background

    自动取消

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

    .setAutoCancel(true)
    

    补充说明

    对于小米、红米等部分设备,需要进入设置开启浮动通知

    【讨论】:

    • 它修复了通知没有消失但没有提醒的问题仍然存在的问题
    • 您可能对 fcm 在后台或前台的行为感兴趣:stackoverflow.com/questions/42315296/…
    • 你的payload既有通知又有数据?
    • @TiagoVitorino 我已经更新了我的答案,请检查
    猜你喜欢
    • 2022-01-10
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 2020-06-19
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    相关资源
    最近更新 更多