gpt4 book ai didi

android - 在 Android O 上禁用通知声音

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:53:03 32 4
gpt4 key购买 nike

我尝试用下面的方法摆脱通知声音

我能够将它减少到只关闭一次,但它应该在 Android O 和更低版本中完全静音。

我在 stackoverflow 和谷歌上搜索了很长时间,但直到现在都没有完全有效。

感谢任何帮助。

public void showUpdateProgressNotification(int id, String appName, int progress, String status, long downloadStart) {

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel
(NOTIFICATION_CHANNEL_ID, "Test Notifications", NotificationManager.IMPORTANCE_LOW);
notificationChannel.setSound(null, null);

// Configure the notification channel.
notificationChannel.setDescription("Channel test");
notificationManager.createNotificationChannel(notificationChannel);
}

Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
cancelIntent.putExtra("id", id);
PendingIntent cancel = PendingIntent.getBroadcast(this, ACTION_FILE_CANCEL.hashCode() + id,
cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(appName)
.setContentText(status)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setDefaults(0)
.setLargeIcon(BitmapFactory.decodeResource(MyApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
.setProgress(100, progress, progress == 0)
.setWhen(downloadStart)
.setContentIntent(cancel)
.setGroup(GROUP_KEY)
.addAction(R.drawable.ic_close_black_24dp, "Cancel", cancel)
.setColor(MyApplication_.getInstance().getResources().getColor(R.color.apps_color))
.setOnlyAlertOnce(true);

NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
inboxStyle.addLine(status);
notification.setStyle(inboxStyle);

notificationManager.notify(id, notification.build());

addNotification(id);
}

最佳答案

经过更多研究,我明白了。

这是关键部分:

//Configure the notification channel, NO SOUND
notificationChannel.setDescription("no sound");
notificationChannel.setSound(null,null); <---- ignore sound
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(false);

一开始实现时,它仍然无法正常工作,但卸载我的应用并重新安装后一切正常。

如果有人需要,这里是正确的代码:

public void showUpdateProgressNotification(int id, String appName, int progress, String status, long downloadStart) {

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "My app no sound", NotificationManager.IMPORTANCE_LOW
);

//Configure the notification channel, NO SOUND
notificationChannel.setDescription("no sound");
notificationChannel.setSound(null,null);
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
}

Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
cancelIntent.putExtra("id", id);
PendingIntent cancel = PendingIntent.getBroadcast(this, ACTION_FILE_CANCEL.hashCode() + id,
cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(appName)
.setContentText(status)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setDefaults(0)
.setLargeIcon(BitmapFactory.decodeResource(MyApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
.setProgress(100, progress, progress == 0)
.setWhen(downloadStart)
.setContentIntent(cancel)
.setGroup(GROUP_KEY)
.addAction(R.drawable.ic_close_black_24dp, "Cancel", cancel)
.setColor(MyApplication_.getInstance().getResources().getColor(R.color.apps_color));

NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
inboxStyle.addLine(status);
notification.setStyle(inboxStyle);

notificationManager.notify(id, notification.build());

addNotification(id);
}

关于android - 在 Android O 上禁用通知声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48453189/

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