【发布时间】:2018-09-28 15:13:03
【问题描述】:
我正在尝试在我的应用中显示通知。一切似乎都很好,但在 Android 8.0 Oreo 中,如果我终止应用程序,则不会显示通知。在以前的 Android 版本中,这不是问题。
我的源代码是下一个。
public class NotificationHelper extends ContextWrapper {
private static final int NOTIFICATION_ID = 600;
private static final String NOTIFICATION_CHANNEL_ID = "bmedio_v6"; // This is the Notification Channel ID. More about this in the next section
private static final String NOTIFICATION_CHANNEL_NAME = "medio"; //User visible Channel Name
private static final String NOTIFICATION_CHANNEL_DESC = "Medio Channel"; //User visible Channel Name
public NotificationHelper(Context base) {
super(base);
}
public void showNotification(String title, String message) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription(NOTIFICATION_CHANNEL_DESC);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
}else{
notificationBuilder = new NotificationCompat.Builder(this);
}
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_icon_notify)
// .setPriority(Notification.PRIORITY_MAX)
.setContentTitle(title)
.setContentText(message);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
}
【问题讨论】:
-
您使用哪款手机..??
标签: android notifications android-8.0-oreo