gpt4 book ai didi

android - 在通知 channel 中启用声音按钮

转载 作者:太空狗 更新时间:2023-10-29 13:46:32 24 4
gpt4 key购买 nike

MI Note 5 Pro中,它有最新的MI UI 10.0 with Oreo,所以当我尝试默认发送推送通知声音被禁用,所以当我为此创建 channel 时,我无法以编程方式启用声音

在其他 Oreo 设备中会发出通知声音,但在 MI 自定义 Oreo OS 声音默认情况下禁用

让我展示我的通知代码:

    var intent = Intent(mContext, HomeActivity::class.java)
intent.putExtra(Constants.EXTRA_FROM_NOTIFICATION, true)
var pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

var uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

var mBuilder = NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
.setContentTitle(mContext.getString(R.string.app_name))
.setContentText(mFirstContactName + " : " + mListChatWindow[0].message)
.setPriority(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) NotificationManager.IMPORTANCE_HIGH else Notification.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setSound(uri)
.setSmallIcon(R.drawable.ic_app_icon)
.setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
.setVibrate(longArrayOf(0, 100, 1000, 300))
.setAutoCancel(true)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
var channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH)
channel.description = "NOTIFICATION_DESCRIPTION"
channel.lightColor = Color.LTGRAY
channel.enableVibration(true)
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC

val attributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build()
channel.setSound(uri, attributes)
var notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}

var notificationManager = NotificationManagerCompat.from(mContext)
notificationManager.notify(NOTIFICATION_CHAT_ID, mBuilder.build())

我也在 cannel 中设置了 channel.setSound(uri, attributes) 但是没有声音

这是通知 channel 的截图,看到声音图标被禁用了,如何启用?

请帮忙

enter image description here

最佳答案

我在使用 oreo 的 MIUI Global 10.1 中遇到了类似的问题,但它只是在使用自定义声音时出现,而不像你的默认声音。不管怎样,让我解释一下我是如何解决它的,希望它能解决你的问题。

首先要考虑的是在何处执行通知 channel 的注册。它必须在 Application.onCreate 中执行,以便在通知到达之前创建 channel 。我是在 onMessageReceived 中做的。

其次,正如我所说,它适用于默认通知声音而不适用于自定义声音,我在创建通知 channel 时插入了下面的代码并且它起作用了。

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID);

if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.O) {
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND); // This line did the magic for me.

Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound_notification_plucky);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();

CharSequence name = "MyChild";
String description = "All MyChild messages";
int importance = NotificationManager.IMPORTANCE_HIGH;

NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
notificationChannel.setDescription(description);
notificationChannel.enableVibration(true);
notificationChannel.setSound(sound, audioAttributes);

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}

关于android - 在通知 channel 中启用声音按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53150950/

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