android - FCM 通知不显示在后台弹出的通知

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

我正在使用 firebase 云功能在我的应用程序中显示通知,并且工作完美,我面临的唯一问题是当应用程序关闭或在后台通知不显示弹出时。 应用程序中使用了平视通知,我确实收到了通知,但通知不会像应用程序位于前台时那样弹出。我已将 channel 和通知的优先级设置为高,但仍然不起作用。

我的服务等级:

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        String channelId = "Default";
        NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.app_logo)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody())
                .setAutoCancel(true).setContentIntent(pendingIntent)
                .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
                .setPriority(Notification.PRIORITY_MAX);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_HIGH);
            channel.setShowBadge(true);
             channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            manager.createNotificationChannel(channel);
        }
        manager.notify(0, builder.build());
    }

list

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name="com.noderon.riscalert.WebViewActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.noderon.riscalert.MainActivity">

        </activity>

        <service
            android:name=".NotificationService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

    </application>

如有任何帮助,我们将不胜感激。谢谢

最佳答案

由于您已经设置了优先级,因此您似乎没有添加通知的元数据,并且当应用程序位于后台时,它不会获取任何数据,这就是它只在前台显示弹出窗口的原因 在您的代码中添加这些元数据:

<meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_stat_ic_notification" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id" />

此外,您还可以看到Google Cloud Messaging文档以获取更多详细信息

快乐编码

关于android - FCM 通知不显示在后台弹出的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68544416/

相关文章:

java - 如何将图像移动到触摸位置?

android - 如何解决这个 Proguard 问题?

java - 从上次中断的地方继续?

firebase - 在 Golang 中仅使用环境变量初始化 Firebase Admin SDK

android - Realm 对象和反序列化包含具有整数和字符串的数组的数组,以便与 Realm 一起使用

firebase - AngularFIRE 属性 'subscribe' 在类型 'AngularFireList<{}>' 上不存在

javascript - Chrome 上的 Firebase 云消息传递未经授权错误 401

ios - 推送通知本地化以发送设备语言显示,而不是接收设备语言。 swift

ios - flutter : Unable to redirect to specific screen on Notification click when app is in background but not terminated in iOS