Java 通知声音不在Android中播放

Java 通知声音不在Android中播放,java,android,notifications,Java,Android,Notifications,创建通知频道 在创建频道之前,我正在删除具有频道id的旧频道 public class App extends Application { public static String CHANNEL_ID = "CH1"; public static String CHANNEL_NAME = "CH1 Channel"; @Override public void onCreate() { super.o

创建通知频道 在创建频道之前,我正在删除具有频道id的旧频道

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);
在创建频道之前,我正在删除具有频道id的旧频道。 我不知道我在这个代码中哪里出错了


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

设置声音只是设置铃声。只需添加setonlylertonce(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();
查看详细信息