Android:通知不会生成默认声音

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android:通知不会生成默认声音相关的知识,希望对你有一定的参考价值。

我试图用声音生成FCM通知。我收到通知等没有问题,但根本没有声音。我可以使用默认的通知声音。请检查以下代码。它适用于API 26及更高版本。

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

                // The id of the channel.
                String id = "xxx";

                // The user-visible name of the channel.
                CharSequence name = "xxx";

                // The user-visible description of the channel.
                String description = "xxx";

                int importance = NotificationManager.IMPORTANCE_DEFAULT;

                NotificationChannel mChannel = new NotificationChannel(id, name, importance);

                // Configure the notification channel.
                mChannel.setDescription(description);

                mChannel.enableLights(true);
                // Sets the notification light color for notifications posted to this
                // channel, if the device supports this feature.
                mChannel.setLightColor(Color.RED);

                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});


                mNotificationManager.createNotificationChannel(mChannel);

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



                // The id of the channel.
                String CHANNEL_ID = "xxx";

                // Create a notification and set the notification channel.
                Notification notification = new Notification.Builder(this,"xxx")
                        .setSmallIcon(R.drawable.volusha_notifications)
                        .setContentText(text)
                        .setChannelId(CHANNEL_ID)
                        .setContentIntent(pendingIntent)
                        .setContentTitle(title)
                        .setAutoCancel(true)
                        .build();

                // Issue the notification.
                mNotificationManager.notify(new Random().nextInt(), notification);

为什么会发生这种情况以及如何获得默认声音?

答案

改变这一部分:

// Create a notification and set the notification channel.
                Notification notification = new Notification.Builder(this,"xxx")
                        .setSmallIcon(R.drawable.volusha_notifications)
                        .setContentText(text)
                        .setChannelId(CHANNEL_ID)
                        .setContentIntent(pendingIntent)
                        .setContentTitle(title)
                        .setAutoCancel(true)
                        .build();

// Create a notification and set the notification channel.
                Notification notification = new Notification.Builder(this,"xxx")
                        .setSmallIcon(R.drawable.volusha_notifications)
                        .setContentText(text)
                        .setChannelId(CHANNEL_ID)
                        .setContentIntent(pendingIntent)
                        .setContentTitle(title)
                        .setSound(RingtoneManager
                              .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setAutoCancel(true)
                        .build();
另一答案

尝试使用RingtoneManager获取默认通知Uri为:

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(uri);

以上是关于Android:通知不会生成默认声音的主要内容,如果未能解决你的问题,请参考以下文章

Android系统修改默认声音及大小

Android系统修改默认声音及大小

Android默认系统声音/大小修改及配置

Worklight 6.1 中的默认推送通知声音

Xamarin.Android 如何使“wav”文件成为应用程序的默认推送通知声音?

iOS 7 本地通知默认没有声音