【问题标题】:Firebase Notification is not working on Android OFirebase 通知不适用于 Android O
【发布时间】:2017-10-17 07:13:32
【问题描述】:

我正在使用 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

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

所以在做完这一切之后。我仍然在日志中收到该错误消息,并且无法从控制台接收通知。任何帮助都会很重要。

【问题讨论】:

  • 投反对票有什么原因吗?
  • 您是否看到From: 日志消息表明已收到消息?
  • 当应用程序在前台时,我可以看到。但是在应用程序进入后台后,它会显示错误。@BobSnyder
  • Firebase support for notification channels 是在 10.2.6 版中添加的。您正在使用该版本或更高版本进行构建吗?
  • @Tej 你能帮我解决同样的问题吗.....你是如何解决项目中的问题的?您在项目中的哪里创建了通知通道?

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


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2018-02-02
    • 2018-12-15
    • 1970-01-01
    • 2020-05-22
    • 2018-05-25
    • 1970-01-01
    相关资源
    最近更新 更多