gpt4 book ai didi

android - 如何在 Oreo 或更高版本中显示抬头通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:19:34 26 4
gpt4 key购买 nike

我认为提醒通知在 android 8.0 中禁用,但 android 的内置消息即使在运行 android 8.0 时也具有此功能。我知道如何通过将振动设置为通知来显示低于 8.0 设备的抬头通知。但是通知运行奥利奥的振动已被弃用。所以我去互联网上搜索,有人说启用通知 channel 的 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);

最佳答案

迟到的答案:- 但我认为它值得分享。 我正在做与上面相同的事情,但 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,我才明白这一点.

Importance levels have the following restrictions:

The importance level you assign will be the channel’s default. Users can change a channel’s importance level in Android Settings. Once you choose an importance level, you're limited in how you can change it: you may only lower the importance, and only if the user hasn't explicitly changed it.

结论:- 在升级通知 channel 的重要性之前完全卸载以前的版本或清除数据(对此不确定)。

关于android - 如何在 Oreo 或更高版本中显示抬头通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47969923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com