【问题标题】:Android 8 notifications setSound not workingAndroid 8 通知设置声音不起作用
【发布时间】:2017-09-17 20:39:41
【问题描述】:

我有以下代码,但每次我只听到默认的 android 声音。

        // create  channel
        NotificationChannel channel = new NotificationChannel(ANDROID_CHANNEL_ID,
                ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        // Sets whether notifications posted to this channel should display notification lights
        channel.enableLights(true);
        // Sets whether notification posted to this channel should vibrate.
        channel.enableVibration(true);
        // Sets the notification light color for notifications posted to this channel
        channel.setLightColor(Color.GREEN);
        // Sets whether notifications posted to this channel appear on the lockscreen or not
        //channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        Uri uri = Uri.parse("android.resource://"+this.getPackageName()+"/" + R.raw.aperturaabductores);

        AudioAttributes att = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .build();
        channel.setSound(uri,att);

这是我的声音pablomonteserin.es/aperturaabductores.wav

【问题讨论】:

  • 经过测试。在模拟器中工作。
  • 在我的模拟器和设备中也不起作用。你在 Android 8 上测试过吗?请注意,我想加载自定义声音。
  • 我用我的自定义声音做了,我知道它是 8。提供声音,我会尝试的。
  • 对我来说不同的是在频道上设置了android oreo的声音文件。

标签: android android-notifications android-8.0-oreo


【解决方案1】:

我试图查看您的声音文件和我的声​​音文件之间的区别。我用的是 Audacity 软件。 您的声音文件的采样率为 22050Hz,而我使用的声音文件的采样率为 44100Hz。因此,我将您的声音文件采样率转换为 44100Hz 并将其用作通知声音。现在可以了。

问题出在声音文件上。可能是 Android O 的新变化,因为它在旧 Android 版本上运行良好。

这是重新采样的方法-

【讨论】:

  • 你能发布你的代码吗,因为我在 8.1.0 中仍然有问题
  • 我尝试了所有可能的解决方案,经过数小时的尝试,这救了我!你救了我的命!! :) 尝试时记得卸载应用,否则通知频道不会更新...谢谢老兄!
【解决方案2】:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Notification.Builder notificationBuilder =
                    new Notification.Builder(MyApplication.getInstance().getApplicationContext(), NOTIFICATION_CHANNEL_ID)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(pTitle)
                            .setContentText(messageBody)
                            .setAutoCancel(true)
                            //.setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
                            //.setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);

            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
            // Configure the notification channel.
            AudioAttributes att = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                    .build();
            notificationChannel.setSound(defaultSoundUri,att);
            notificationChannel.setDescription(messageBody);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notificationChannel);
            if (imageThumbnail != null) {
                notificationBuilder.setStyle(new Notification.BigPictureStyle()
                        .bigPicture(imageThumbnail).setSummaryText(messageBody));
            }
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

        } else {
            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(MyApplication.getInstance().getApplicationContext())
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(pTitle)
                            .setContentText(messageBody)
                            .setAutoCancel(true)
                            .setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);
            if (imageThumbnail != null) {
                notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle()
                        .bigPicture(imageThumbnail).setSummaryText(messageBody));
            }
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

        }

【讨论】:

  • 什么是defaultSoundUri?]
  • Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-05
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多