[英]FCM notifications not working in Android latest version OREO
FCM notifications are not working in Android latest version OREO. FCM通知在Android最新版本OREO中无法使用。
Below is my code: 以下是我的代码:
AndroidManifest.xml 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>
From Android 8.0 (API level 26+), notification channels are supported and recommended. 从Android 8.0(API级别26+),支持并推荐通知渠道。
FCM provides a default notification channel with basic settings.
FCM提供具有基本设置的默认通知通道。
If you prefer to create and use your own default channel, set default_notification_channel_id
to the ID of your notification channel object as shown below. 如果您更喜欢创建和使用自己的默认频道,请将
default_notification_channel_id
设置为通知频道对象的ID,如下所示。
FCM will use this value whenever incoming messages do not explicitly set a notification channel.
只要传入的消息未明确设置通知通道,FCM就会使用此值。
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
In Android Oreo making channel is important,When yo create notification you must pass the channel id,or your notification won't show up in Oreo devices. 在Android Oreo制作频道很重要,当您创建通知时,您必须传递频道ID,否则您的通知将不会显示在Oreo设备中。
quick fix is: use setChannelId method in notification class. 快速修复:在通知类中使用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);
You need to create channels in order to display a notification in Oreo
. 您需要创建频道才能在
Oreo
显示通知。
If you use default firebase notifications, you can add channel_id
in manifest. 如果使用默认的firebase通知,则可以在清单中添加
channel_id
。
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
If you create notifications manually, then you need to create channel programmatically. 如果手动创建通知,则需要以编程方式创建通道。 More details on official documentation .
关于官方文档的更多细节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.