我认为Android 8.0中的抬头通知禁用,但即使在运行Android 8.0时,android的内置消息也具有此功能。 我知道如何通过设置振动通知来显示低于8.0设备的抬头通知。但是,运行Oreo的通知上的振动已被弃用。所以我去互联网搜索,有人说启用setVibrationPattern到通知通道将起作用。但遗憾的是,这不起作用。
所以这是我的代码。
Notification builder = new NotificationCompat.Builder(MainActivity.this)
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_android_black_24dp)
.setChannelId(id)
.setPriority(Notification.PRIORITY_HIGH)
.setColor(setActionColor(counter))
.setColorized(true)
.setContentIntent(pendingIntent)
.build();
//Notification channel code.
NotificationChannel chan2 = new NotificationChannel(SECONDARY_CHANNEL,
getString(R.string.noti_channel_second), NotificationManager.IMPORTANCE_HIGH);
chan2.setLightColor(Color.BLUE);
chan2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
manager.createNotificationChannel(chan2);
manager.notify(notificationID, builder);
答案 0 :(得分:9)
最新答案:-但我认为值得分享。
我在做与上述相同的事情,但是Heads-up notifications
在奥利奥(Oreo)中不会显示。
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel("ch_nr", "CHART",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setVibrationPattern(new long[]{300, 300, 300});
if (defaultSoundUri != null) {
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
notificationChannel.setSound(defaultSoundUri, att);
}
notificationManager.createNotificationChannel(notificationChannel);
notificationBuilder = new NotificationCompat.Builder(context, notificationChannel.getId());
}
基本代码在上方。
问题是我每次都覆盖设备上的构建,而importance
并没有变为HIGH。直到碰到这个document,我才明白这一点。
重要级别具有以下限制:
您分配的重要性级别将是频道的默认级别。用户可以在“ Android设置”中更改频道的重要性级别。 选择重要性级别后,您将无法更改它:只有在用户未明确更改重要性的情况下,您才可以降低重要性。
结论:-在升级通知通道的重要性之前,请完全卸载以前的版本或清除数据(对此不确定)。
答案 1 :(得分:5)
您可以这样做。.这将适用于每个Android版本
private NotificationManager notifManager;
public void createNotification(String aMessage) {
final int NOTIFY_ID = 1002;
// There are hardcoding only for show it's just strings
String name = "my_package_channel";
String id = "my_package_channel_1"; // The user-visible name of the channel.
String description = "my_package_first_channel"; // The user-visible description of the channel.
Intent intent;
PendingIntent pendingIntent;
NotificationCompat.Builder builder;
if (notifManager == null) {
notifManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = notifManager.getNotificationChannel(id);
if (mChannel == null) {
mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableVibration(true);
mChannel.setLightColor(Color.GREEN);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notifManager.createNotificationChannel(mChannel);
}
builder = new NotificationCompat.Builder(this, id);
intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentTitle(aMessage) // required
.setSmallIcon(android.R.drawable.ic_popup_reminder) // required
.setContentText(this.getString(R.string.app_name)) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setTicker(aMessage)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
} else {
builder = new NotificationCompat.Builder(this);
intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentTitle(aMessage) // required
.setSmallIcon(android.R.drawable.ic_popup_reminder) // required
.setContentText(this.getString(R.string.app_name)) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setTicker(aMessage)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setPriority(Notification.PRIORITY_HIGH);
} // else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification notification = builder.build();
notifManager.notify(NOTIFY_ID, notification);
}
答案 2 :(得分:3)
您还需要将“设置”中的应用程序通知设置设置为“紧急”。除非用户将应用程序设置为紧急,否则不会显示抬头通知。
答案 3 :(得分:1)
如果您创建通知频道并执行此类操作,则抬头通知会有效。
NotificationChannel chan2 = new NotificationChannel(SECONDARY_CHANNEL,
getString(R.string.noti_channel_second), NotificationManager.IMPORTANCE_HIGH);
chan2.setLightColor(Color.BLUE);
chan2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(chan2);
答案 4 :(得分:0)
您设置了错误的频道ID。
setChannelId(String channelId)指定通知通道 应该交付。
在您的示例中,您正在将id
设置为通知生成器:
setChannelId(id)
然后创建一个您未指定的新辅助频道:chan2
这是我的工作示例:
private static final String CHANNEL_ID = "CHANNEL_NO_1";
public void sendNotification(Context context) {
// Create your notification channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "New riddle";
String description = "Riddle of the day";
int importance = NotificationManager.IMPORTANCE_HIGH;
// IMPORTANT: CHANNEL_ID
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
// Get an instance of NotificationManager
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle("Riddle of the day:")
//IMPORTANT: CHANNEL_ID
.setChannelId(CHANNEL_ID)
// It won't show "Heads Up" unless it plays a sound
if (Build.VERSION.SDK_INT >= 21) mBuilder.setVibrate(new long[0]);
// Gets an instance of the NotificationManager service//
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(001, mBuilder.build());
}
还有一件事情,请确保在进行这些更改后重新重新安装您的应用。
答案 5 :(得分:0)
在某些手机中,有一个状态栏和通知设置,如果通知的顶部预览设置为无预览,则它将不允许任何应用程序抬起头来进行设置横幅可以解决您的问题,前提是您已正确设置了频道和通知。
我在使用Android 9 OS Vivo y1902手机时遇到了同样的问题,在进行了上述更改之后,该问题得以解决。