android - Firebase 通知不适用于 Android O

标签 android firebase push-notification android-8.0-oreo

我正在使用 Android O 模拟器并希望从 Firebase 控制台获取通知。它在除 Android O 之外的所有设备上都运行良好。我在日志中收到此错误。

W/Notification: Use of stream types is deprecated for operations other than volume control
W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

我知道我必须为此指定 channel ID。 所以到目前为止我做了什么

AndroidManifest.xml

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

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


            <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="my_notification_channel" />

            <service android:name=".Helper.FirebaseIDService">
                <intent-filter>
                    <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
                </intent-filter>
            </service>

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

       <my all activities...>

我还像这样在 FirebaseMessagingService 中指定了 channel ID

 public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    private static final int NOTIFICATION_ID = 1;
    private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);

            // Configure the notification channel.
            notificationChannel.setDescription("Channel description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }


        NotificationCompat.Builder mBuilder;
        mBuilder = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Title")
                .setContentText(remoteMessage.getNotification().getBody())
                .setOngoing(true)
                .setChannelId(NOTIFICATION_CHANNEL_ID);

        notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
 }
}

所以在完成所有这些之后。我仍然在日志中收到该错误消息,并且无法从控制台接收通知。任何帮助都将不胜感激。

最佳答案

当 FCM 消息不包含数据负载,仅包含通知负载,并且您的应用程序在后台时,Firebase 会直接生成通知并且不会调用 onMessageReceived()。这在 the documentation 中有解释。 .

对于 Android O,必须创建一个通知 channel 。您在 list 中正确定义了默认通知 channel ,但该 channel 是在 onMessageReceived() 代码中创建的,该代码在应用程序处于后台时不会执行。

将创建 channel 的代码移动到您可以确定它会在收到通知之前执行的位置。

此外,如 Firebase Release Notes 中所示,在 10.2.6 版本中添加了对通知 channel 的支持。您必须至少使用该版本进行构建。

关于android - Firebase 通知不适用于 Android O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46784463/

相关文章:

java - 如何显示变量的值? - 安卓

firebase - 设置最小密码长度Firebase电子邮件和密码身份验证

iPhone 推送通知信息

Android BLE 无法从设备接收 Gatt 特性通知

android - 如何修改 android 源代码并将其用于 Android 设备?

Android EditText MultiLine 与句子大写

swift - 子类类型作为闭包参数

javascript - Firestore 安全规则 - 不通过身份验证或写入 (resource.data) 将数据传递到 Firestore

php - Service Worker 中的 XMLHttpRequest

Android:如何在没有来自android的请求的情况下将数据从服务器发送到android?