android - 如何在android 8.0中设置带有自定义声音的通知?

标签 android push-notification android-8.0-oreo

我尝试设置自定义通知声音,它在 Oreo(8.0) 设备下工作正常,但在 Oreo 设备中它不会触发自定义声音,始终只触发默认声音。谁能帮忙?

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Notification.Builder notificationBuilder =
                    new Notification.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(title)
                            .setContentText(content)
                            .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(content);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notificationChannel);
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

        } else {
            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(getApplicationContext())
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(title)
                            .setContentText(content)
                            .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);

            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

        }

最佳答案

如果您使用的是 Oreo 下的 Android 版本,您只需将 MY_SOUND_NOTIFICATION.mp3 添加到名为 raw 的文件夹中的Resources,然后开始编写您的代码类,但由于您使用的是 Oreo 或更高版本,因此您需要检查您的 SDK_VERSION 以便使用 NotificationChannel

的名为 setSound 的方法

我不会发布任何代码,因为我没有发布代码来更正。如果您发布一些代码,我很乐意为您提供帮助

关于android - 如何在android 8.0中设置带有自定义声音的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52757119/

相关文章:

android - ionic 2 : camera and photo gallery together

ios - [iOS]更新推送证书但没有certificateSigningRequest文件

ios - 使用 Swift 的 Firebase 推送通知在 ios 11.4 中不起作用

android - 在 Android O 中发送本地通知

Android 6.0 权限错误

android - 我在 BOOT_COMPLETED 中遇到问题

android - 菜单项未检测到用户点击 action_finish 按钮

android - Android 库可以接收推送通知吗?

Android "O"(Oreo, 8) 和更高版本的媒体按钮问题

android - PendingIntent 不适用于 Android O