【问题标题】:FCM notification doesn't show notification pop up in backgroundFCM 通知不显示在后台弹出通知
【发布时间】:2021-07-27 11:59:15
【问题描述】:

我正在使用 Firebase 云功能在我的应用程序中显示通知,而我面临的唯一问题是当应用程序关闭或后台通知不显示弹出时。 应用程序中使用了抬头通知,我确实收到了通知,但通知没有像应用程序处于前台时那样弹出。我已将频道和通知的优先级设置为高,但它仍然不起作用。

我的服务等级:

@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());
    }

清单

<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>

任何帮助将不胜感激。谢谢

【问题讨论】:

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


    【解决方案1】:

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

    <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 文档了解更多详情

    愉快的编码

    【讨论】:

    • 感谢工作我丢失了元数据。我看到我的 logcat 甚至 android studio 都给我元数据丢失或 null 的错误
    【解决方案2】:

    我也遇到过同样的问题。我遵循了 firebase 的官方文档,现在通知工作正常。关注this link .

    【讨论】:

      【解决方案3】:

      您是否在 Activity 中创建了 FirebaseMessage Instance()?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-15
        • 2022-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-27
        相关资源
        最近更新 更多