繁体   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