簡體   English   中英

通知聲音不在 Android 中播放

[英]Notification Sound Not Playing In Android

在創建頻道之前創建通知頻道我正在刪除帶有頻道 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 的舊頻道。 我不知道我在這段代碼中哪里出錯了。

如果我從頻道中刪除設置聲音的代碼並且通知默認聲音沒有播放但其他通知有聲音!。 我嘗試將重要的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();

查看文檔了解詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM