通知自定义声音和振动不起作用?

编程入门 行业动态 更新时间:2024-10-11 01:20:09
本文介绍了通知自定义声音和振动不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个接收通知的应用程序,一切正常.但是,自定义声音和振动不起作用.我正在Android 9 Pie上对其进行测试.

I have an app that receives notification, all is working. However, the custom sound and vibration is not working. I'm testing it on Android 9 pie.

Uri sound = Uri.parse("android.resource://" +getApplicationContext().getPackageName()+ "/" +R.raw.siren); //ContentResolver.SCHEME_ANDROID_RESOURCE + String NOTIFICATION_CHANNEL_ID = "com.example.bantay.bantay.test"; NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); notificationBuilder.setAutoCancel(true); notificationBuilder.setWhen(System.currentTimeMillis()); notificationBuilder.setSmallIcon(R.drawable.marikinalogo); notificationBuilder.setContentTitle(title); notificationBuilder.setContentText(body); notificationBuilder.setSound(sound); notificationBuilder.setVibrate(new long[]{500, 1000, 500, 1000}); if(title.toLowerCase().contains("1")){ notificationBuilder.setContentIntent(pendingIntent); } else{ notificationBuilder.setContentIntent(pendingIntent3); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_HIGH); AudioAttributes audioAttributes = new AudioAttributes.Builder() .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .setUsage(AudioAttributes.USAGE_ALARM) .build(); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.enableVibration(true); notificationChannel.setVibrationPattern(new long[]{500, 1000, 500, 1000}); notificationChannel.setSound(sound, audioAttributes); notificationManager.createNotificationChannel(notificationChannel); } notificationManager.notify(0, notificationBuilder.build());

我不知道我的错误.NOTIFICATION_CHANNEL_ID是否会影响通知的行为?

I don't know my errors. Does the NOTIFICATION_CHANNEL_ID affect the behavior of the notification?

推荐答案

我正在使用 MediaPlayer 自定义声音.这对我来说很好.这适用于所有设备.

I am using MediaPlayer for custom sound. And This is Working fine for me. This is working for all devices.

private MediaPlayer player;

要播放自定义声音:

try { Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.siren); player = MediaPlayer.create(this, uri); player.setLooping(true); // This will play sound in repeatable mode. player.start(); // mBuilder.setSound(uri); } catch (Exception e) { e.printStackTrace(); }

停止声音:

if (player != null) player.stop();

这对我有用.希望这也会对您有所帮助.

This is working for me. Hope this will also helps you.

更多推荐

通知自定义声音和振动不起作用?

本文发布于:2023-11-02 06:01:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551562.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   不起作用   声音   通知

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!