gpt4 book ai didi

java - 奥利奥和馅饼通知

转载 作者:行者123 更新时间:2023-11-30 10:06:09 25 4
gpt4 key购买 nike

问题:我的应用程序需要可由用户自定义的通知。例如。为通知设置不同的声音、标题、文本

问题:我知道通知 channel 只设置一次并且不能修改,所以我想我可以只使用变量,但是即使使用变量,一旦我选择了一个声音它就会保持那个声音并且根本无法改变。我能想到的唯一解决方案是每次更改看起来很愚蠢的东西时都会有一个新 channel 。当然还有另一种方法??

此外,在 Android Pie 上,通知声音根本不起作用,我不明白为什么。

Uri soundUri = Uri.parse(notificationSound);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(customTextTitle)
.setContentText(customTextBody)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

if(soundUri != null){
// Changing Default mode of notification
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
// Creating an Audio Attribute
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();

// Creating Channel
NotificationChannel notificationChannel = new NotificationChannel("CH_ID","Testing_Audio",NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(soundUri,audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
mNotificationManager.notify(0, notificationBuilder.build());
}

最佳答案

我已经解决了这个问题,尽管是以一种 hacky 的方式:( 我最终将声音设置为 null if >=Oreo 并使用媒体播放器播放通知声音并将 audioStream 设置为 STREAM_NOTIFICATION。显然不理想,但作为这是一个通知而不是警报,音频不应超过在 onReceive 期间设置的执行任务的时间。

我可以看到此解决方案的唯一问题是用户是否决定将通知静音/调整手机上的 channel 。在那里静音显然不会影响媒体播放器播放的声音,而且很可能会显示为没有声音,这很不幸。

 try {
mp.setDataSource(context.getApplicationContext(), soundUri);
mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mp.prepare();
mp.start();
} catch (Exception e) {
//exception caught in the end zone
}

对于遇到此问题的任何其他人,我在这里找到了一个非常棒的帖子,它与我的解决方案相同,但更强大,更深入。 Android O - Notification Channels - Change Vibration Pattern or Sound Type

关于java - 奥利奥和馅饼通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54804290/

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