gpt4 book ai didi

android 通知 channel 声音不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:20:33 24 4
gpt4 key购买 nike

我知道有很多关于这个问题的帖子。我都试过了。这是我执行的步骤。

首先我发现 channel 一旦创建就无法更改。唯一的办法是重新安装应用程序。所以这就是我所做的,但没有奏效。

其次,有人说我可以删除 channel ,所以我也使用这段代码来删除

val channelList = mNotificationManager.notificationChannels
var i = 0
while (channelList != null && i < channelList.size) {
Log.d("channelList","channel ID is ${channelList[i].id}")
//mNotificationManager.deleteNotificationChannel(channelList[i].id)
i++
}

删除后重新创建 channel 。

第三,我尝试使用新的通知 channel ,但每次使用新 channel 时都会出现错误。

这是我在尝试过的所有这些解决方案中使用的代码

 val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()


val importance = NotificationManager.IMPORTANCE_DEFAULT

val channelList = mNotificationManager.notificationChannels
var i = 0
while (channelList != null && i < channelList.size) {
Log.d("channelList","channel ID is ${channelList[i].id}")
mNotificationManager.deleteNotificationChannel(channelList[i].id)
i++
}

Log.d("isnotification"," is it needed $isNotificationSoundNeeded importance is $importance")
val mChannel = NotificationChannel(CHANNEL_ID, appName, NotificationManager.IMPORTANCE_HIGH)
mChannel.setShowBadge(false)
mChannel.setSound(notifSound, audioAttributes)



val mChannelnew = NotificationChannel(CHANNEL_ID2, appName, NotificationManager.IMPORTANCE_DEFAULT)
mChannelnew.setShowBadge(false)
mChannelnew.setSound(notifSound, audioAttributes)




mNotificationManager.createNotificationChannel(mChannel)

我错过了什么?有任何想法吗?谢谢

更新:这里是notifsound的代码

val notifSound = Uri.parse("android.resource://" + packageName + "/" + R.raw.unconvinced)

最佳答案

首先,我不知道你的通知在OreoPie 或低于N 的设备上不起作用。

对于你的问题,StackOver Flow 有很多答案。

现在根据你的问题你只遗漏了一行代码但是这里无法检查你的整个通知代码因为你还没有粘贴。

我在这里粘贴一个通知代码,它正好满足您所有的通知要求。 (完整的自定义通知)

带图像的通知

public void createNotificationWithImage(String title,String message ,Bitmap image) {
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

// Custom Sound Uri

Uri soundUri = Uri.parse("android.resource://" + mContext.getApplicationContext()
.getPackageName() + "/" + R.raw.sniper_gun);

mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.notification_icon);

// Pay attention on below line here (NOTE)
mBuilder.setSound(soundUri);

if (image!=null) {
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setLargeIcon(image)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image).setSummaryText(message).bigLargeIcon(null))
.setColor(Color.GREEN)
.setContentIntent(resultPendingIntent);
}
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

现在我正在粘贴通知代码,它可以在 OREO 设备上运行。

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});

if(soundUri != null){
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
notificationChannel.setSound(soundUri,audioAttributes);
}

assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());


below middle braces use for close your method.
}

无图通知

    public void createNotification(String title,String message){
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

Uri soundUri = Uri.parse("android.resource://" + mContext.getApplicationContext()
.getPackageName() + "/" + R.raw.sniper_gun);



mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.notification_icon);
mBuilder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),
R.mipmap.icon));
mBuilder.setSound(soundUri);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setColor(Color.GREEN)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentIntent(resultPendingIntent);

mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
// notificationChannel.s

if(soundUri != null){
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
notificationChannel.setSound(soundUri,audioAttributes);
}

assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);

}

assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());

}

注意:在我的代码中,我提到要注意一个特定的行,其中我描述了要使用通知设置声音 Uri。你可以这样描述。

mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setSound(soundUri)
.setColor(Color.GREEN)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentIntent(resultPendingIntent);

但它不会为您播放声音,因为在 Oreo 设备未将声音设置为优先级之后。

所以总是按照我所描述的声音使用代码。

关于android 通知 channel 声音不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56370123/

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