我正在开发一个 Android 应用程序,它使用 FCM 向应用程序推送通知。 高达 API 29(android Q/10) 后台通知运行良好,但在 api 29 以上它不显示我用于通知的图标。 此外,当 App 处于前台时,它不会在任何 android 版本中显示图标。它只是显示我提到的颜色。
我尝试的所有内容如下代码:
Firebase 消息服务类
public class NotificationService extends com.google.firebase.messaging.FirebaseMessagingService {
int requestId = 1;
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
receiveNotification(remoteMessage);
}
private void receiveNotification(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, UserNameActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestId, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification = new NotificationCompat.Builder(this, FCM_CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notification_icon))
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.colorPrimaryDark))
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(requestId, notification);
}
@Override
public void onNewToken(@NonNull String s) {
}}
应用类
public class App extends Application {
public static final String FCM_CHANNEL_ID = "FCM_CHANNEL_ID";
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(FCM_CHANNEL_ID, "FCM_CHANNEL", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
}
}}
清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.marvel">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name=".utils.App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".activity.UserNameActivity" />
<activity android:name=".activity.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".services.NotificationService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
我不知道我到底错过了什么。 如果有人可以帮助我,请评论或回复此帖子。
先谢谢你!