簡體   English   中英

顯示通知時沒有聲音

[英]No sound when displaying notification

當我顯示通知時,我的應用程序不會播放任何聲音(或振動)。

通知代碼:

Uri sound = Settings.System.DEFAULT_NOTIFICATION_URI;

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channel)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentTitle(title)
                .setContentText(text)
                .setSound(sound, AudioManager.STREAM_NOTIFICATION)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setSmallIcon(R.drawable.small_icon)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                .setContentIntent(contentIntent);

通知管理器:

NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            String channelName = getChannelName(channel);
            Uri sound = Settings.System.DEFAULT_NOTIFICATION_URI;

            AudioAttributes attribute = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                    .build();

            NotificationChannel mChannel = new NotificationChannel(channel, channelName, NotificationManager.IMPORTANCE_DEFAULT);
            mChannel.setSound(sound, attribute);

            if (manager != null) {
                manager.createNotificationChannel(mChannel);
            }
        }

我查看了許多較舊的問題,但沒有找到任何解決方案。

我在 android 9.0.0 上測試了這個

你創建了一個Notification.Builder但你從來沒有創建一個Notification ...使用Builder.build()來獲得一個。 同樣,您獲得了NotificationManager但您永遠不會作為工具來顯示Notification 為此,請使用manager.notify()

您可能為通知通道設置了其他優先級。

暫無
暫無

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

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