java - Android 中不播放通知声音

标签 java android notifications

创建通知 channel 在创建 channel 之前,我将删除带有 channel ID 的旧 channel 。

public class App extends Application {

        public static String CHANNEL_ID = "CH1";
        public static String CHANNEL_NAME = "CH1 Channel";

        @Override
        public void onCreate() {
            super.onCreate();
            createNotificationChannel();
        }

        public void createNotificationChannel() 
        {
            PrefManager prefManager = PrefManager.getPrefManager(this);

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) 
            {
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

                Uri defaultUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

                AudioAttributes attr = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();

                if (prefManager.getNotificationSound().equals("Bell")) {
                Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                        + "://" + getPackageName() + "/" + R.raw.bells);
                channel.setSound(alarmSound, attr);
                } else {
                channel.setSound(defaultUri, attr);
                }

                channel.enableLights(true);
                channel.enableVibration(true);
                assert notificationManager != null;
               //Deleting Notification Channel
                try 
                {
                    notificationManager.deleteNotificationChannel(channel.getId());
                } catch (Exception E) {}
                notificationManager.createNotificationChannel(channel);
            }
         }
    }

使用以下方式发布通知。

            Uri defaultUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                    + "://" + context.getPackageName() + "/" + R.raw.bells);


            Notification notification = new NotificationCompat.Builder(context, App.CHANNEL_ID)
                    .setContentTitle(description)
                    .setContentText(notificationMessageArray[randomIndex])
                    .setSmallIcon(R.drawable.ic_buddha)
                    .setDefaults(Notification.DEFAULT_VIBRATE)
                    .setSound((PrefManager.getPrefManager(context).getNotificationSound().equals("Bell") ? alarmSound : defaultUri), AudioManager.STREAM_NOTIFICATION)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_REMINDER)
                    .build();


            Log.e(TAG, "Posting Notification " + description);

            notificationManager.notify(new Random().nextInt() * 100, notification);

在创建 channel 之前,我将删除带有 channel ID 的旧 channel 。 我不知道这段代码哪里出错了。

如果我从 channel 中删除设置声音的代码,通知默认声音不会播放,但其他通知有声音!。我尝试将重要的 HIGH 更改为 DEFAULT 但结果仍然相同。

最佳答案

setSound仅设置铃声。只需添加 setOnlyAlertOnce(true),您的代码将如下所示:

Notification notification = new NotificationCompat.Builder(context, App.CHANNEL_ID)
    .setContentTitle(description)
    .setContentText(notificationMessageArray[randomIndex])
    .setSmallIcon(R.drawable.ic_buddha)
    .setDefaults(Notification.DEFAULT_VIBRATE)
    .setSound((PrefManager.getPrefManager(context).getNotificationSound().equals("Bell") ? alarmSound : defaultUri), AudioManager.STREAM_NOTIFICATION)
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setCategory(NotificationCompat.CATEGORY_REMINDER)
    .setOnlyAlertOnce(true)
    .build();

检查documentation欲知详情

关于java - Android 中不播放通知声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59004882/

相关文章:

java - android - 带 ScrollView 的并排按钮

Java代码将十进制代码转换为十六进制的问题?

java - 如何让 web-fragment.xml 工作

android - 样式自定义对话框 fragment 不起作用

android - 如何将本地主机 SOAP web 服务连接到我的 android

Android开发通知图标问题

java - 如何声明和实例化一个新的通用数组?

android - Flutter 中的 HTTP 请求

android - 如何隐藏 Activity ?

google-chrome - 正常网页中的 Chrome 富通知?