gpt4 book ai didi

java - Android 中不播放通知声音

转载 作者:行者123 更新时间:2023-12-01 20:02:41 25 4
gpt4 key购买 nike

创建通知 channel 在创建 channel 之前,我将删除带有 channel ID 的旧 channel 。

public class App extends Application {

public static String CHANNEL_ID = "CH1";
public static String CHANNEL_NAME = "CH1 Channel";

@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}

public void createNotificationChannel()
{
PrefManager prefManager = PrefManager.getPrefManager(this);

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

Uri defaultUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

AudioAttributes attr = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();

if (prefManager.getNotificationSound().equals("Bell")) {
Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/" + R.raw.bells);
channel.setSound(alarmSound, attr);
} else {
channel.setSound(defaultUri, attr);
}

channel.enableLights(true);
channel.enableVibration(true);
assert notificationManager != null;
//Deleting Notification Channel
try
{
notificationManager.deleteNotificationChannel(channel.getId());
} catch (Exception E) {}
notificationManager.createNotificationChannel(channel);
}
}
}

使用以下方式发布通知。

            Uri defaultUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + context.getPackageName() + "/" + R.raw.bells);


Notification notification = new NotificationCompat.Builder(context, App.CHANNEL_ID)
.setContentTitle(description)
.setContentText(notificationMessageArray[randomIndex])
.setSmallIcon(R.drawable.ic_buddha)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setSound((PrefManager.getPrefManager(context).getNotificationSound().equals("Bell") ? alarmSound : defaultUri), AudioManager.STREAM_NOTIFICATION)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.build();


Log.e(TAG, "Posting Notification " + description);

notificationManager.notify(new Random().nextInt() * 100, notification);

在创建 channel 之前,我将删除带有 channel ID 的旧 channel 。我不知道这段代码哪里出错了。

如果我从 channel 中删除设置声音的代码,通知默认声音不会播放,但其他通知有声音!。我尝试将重要的 HIGH 更改为 DEFAULT 但结果仍然相同。

最佳答案

setSound仅设置铃声。只需添加 setOnlyAlertOnce(true),您的代码将如下所示:

Notification notification = new NotificationCompat.Builder(context, App.CHANNEL_ID)
.setContentTitle(description)
.setContentText(notificationMessageArray[randomIndex])
.setSmallIcon(R.drawable.ic_buddha)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setSound((PrefManager.getPrefManager(context).getNotificationSound().equals("Bell") ? alarmSound : defaultUri), AudioManager.STREAM_NOTIFICATION)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setOnlyAlertOnce(true)
.build();

检查documentation欲知详情

关于java - Android 中不播放通知声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59004882/

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