gur*_*ath 2 android firebase firebase-cloud-messaging
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>
Run Code Online (Sandbox Code Playgroud)
在 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);
Run Code Online (Sandbox Code Playgroud)
此链接中的完整答案。
| 归档时间: |
|
| 查看次数: |
13233 次 |
| 最近记录: |