繁体   English   中英

FCM通知无法在Android最新版本OREO中使用

[英]FCM notifications not working in Android latest version OREO

FCM通知在Android最新版本OREO中无法使用。

以下是我的代码:

AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example">

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/luncher_logo"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/luncher_logo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".WelcomeActivity">
            <intent-filter>
                <action android:name="com.example.action.MAIN" />

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


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

从Android 8.0(API级别26+),支持并推荐通知渠道。

FCM提供具有基本设置的默认通知通道。

如果您更喜欢创建和使用自己的默认频道,请将default_notification_channel_id设置为通知频道对象的ID,如下所示。

只要传入的消息未明确设置通知通道,FCM就会使用此值。

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

您可以从此链接创建频道ID

在Android Oreo制作频道很重要,当您创建通知时,您必须传递频道ID,否则您的通知将不会显示在Oreo设备中。

更多信息

快速修复:在通知类中使用setChannelId方法。

 Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
  .setSmallIcon(R.drawable.ic_notif_icon)
  .setContentTitle("testTitle")
  .setContentText(messageBody)
  .setAutoCancel(true)
  .setSound(defaultSoundUri)
  .setChannelId("testChannelId") // set channel id
  .setContentIntent(pendingIntent);

完整的答案在这个链接

您需要创建频道才能在Oreo显示通知。

如果使用默认的firebase通知,则可以在清单中添加channel_id

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

如果手动创建通知,则需要以编程方式创建通道。 关于官方文档的更多细节。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM