gpt4 book ai didi

Android O 通知不显示

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:43 30 4
gpt4 key购买 nike

我正在尝试为 Android O 版本实现通知。我已经阅读了有关通知管理器和 channel 的信息。所以 Android O 仍然不想重现通知。在 PostCreate 方法的主要 Activity 上,我写了这个。

    NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "my_channel_01";
String CHANNEL_NAME = "my Channel Name";
int NOTIFICATION_ID = 1;

Intent notificationIntent = new Intent(this, MainActivity.class);

PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


NotificationChannel notificationChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

notificationChannel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

mNotifyManager.createNotificationChannel(notificationChannel);
}
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification myNotification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("You have been notify")
.setContentText("This is your Notifiaction Text")
.setSmallIcon(R.drawable.ic_donut_large_black_24dp)
.setChannel(CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setContentIntent(notificationPendingIntent)
.setAutoCancel(true)
.setSound(alarmSound)
.build();

mNotifyManager.notify(NOTIFICATION_ID, myNotification);
Toast.makeText(this, "accepted", Toast.LENGTH_SHORT).show();

在为第 26 个 API 构建后,它不会创建通知,触发 Toast 消息,并且日志会告诉我:

W/Notification: Use of stream types is deprecated for operations other than volume control W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

如何处理这种错误情况?

更新。经过一些调查,我发现 26 api 在通知生成器中使用了一些小改动。现在它也接受香奈儿。因此,对于 26,使用带有两个参数的生成器。

最佳答案

在创建通知/notificationcompact 对象时传递 channel ID。

Notification.Builder(Context context, String channelId)

NotificationCompat.Builder(Context context, String channelId)

关于Android O 通知不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46002219/

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