【发布时间】:2020-10-12 02:49:22
【问题描述】:
我在我的项目中实现了 Firebase 推送通知。我遇到了一个问题,Nougat 中不显示通知图标,但在其他版本中显示相同的代码。 我进行了很多搜索并尝试了不同的解决方案,例如白色图标,放置在 mipmap 文件夹尺寸中的图标,可绘制文件夹中的相同图标,还尝试了清单代码但未成功仍然在通知面板中显示灰色框。
这是我的通知代码:
private void sendNotification(String title, String msg) {
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "101";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@SuppressLint("WrongConstant")
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_MAX);
//Configure Notification Channel
notificationChannel.setDescription(msg);
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_app)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentTitle(title)
.setAutoCancel(true)
.setSound(defaultSound)
.setContentText(msg)
.setContentIntent(pendingIntent)
//.setStyle(style)
// .setLargeIcon(bitmap)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MAX);
int id = (int) System.currentTimeMillis();
notificationManager.notify(id, notificationBuilder.build());
}
清单文件
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification_app" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
【问题讨论】:
标签: push-notification notifications android-7.0-nougat android-7.1-nougat