Firebase 通知不适用于 Android O

Firebase Notification is not working on Android O

我正在使用 Android O Emulator 并希望从 Firebase 获得通知 console.It 在除 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

我知道我必须为此指定频道 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 中指定了频道 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());
 }
}

所以在完成所有这些之后。我仍然在日志中收到该错误消息,无法收到来自 console.Any 的通知。非常感谢。

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

中对此进行了解释

对于AndroidO,必须创建通知渠道。您在清单中正确定义了默认通知通道,但该通道是在 onMessageReceived() 代码中创建的,当应用程序处于后台时该代码不会执行。

将创建频道的代码移到您可以确定它会在收到通知之前执行的地方。

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