Android 8 Oreo通知无法播放自定义声音

时间:2018-09-11 08:09:45

标签: push-notification android-8.0-oreo

我的应用为传入通知(FCM通知类型)播放自定义声音。对于Android <8,一切正常,在Android 8中,如果我打开了应用程序,它仍然可以播放,如果我关闭了应用程序,则仍然可以播放。 我尝试设置所有内容,更改频道ID的名称和重要性,清除数据并卸载/重新安装应用程序,但无济于事。

这是我与Android O通知相关的代码:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = null;
if (remoteMessage.getNotification().getSound() != null && !remoteMessage.getNotification().getSound().equals("default")) {
    Uri newSoundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                    + "://" + this.getPackageName() + "/raw/" + 
    remoteMessage.getNotification().getSound());
    if (newSoundUri != null)
        defaultSoundUri = newSoundUri;
    else defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    } 
else defaultSoundUri = 
    RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

grantUriPermission("com.android.systemui", defaultSoundUri,
                Intent.FLAG_GRANT_READ_URI_PERMISSION);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = getString(R.string.default_notification_channel_id);
        NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(this, channelId)

        .setSmallIcon(R.drawable.ic_stat_ic_notification)
        .setContentTitle(getString(R.string.app_name))

        .setContentText(remoteMessage.getNotification().getBody())
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setDefaults( Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
                            .setContentIntent(pendingIntent)
                            .setOnlyAlertOnce(false);

            // Creating an Audio Attribute
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            // Since android Oreo notification channel is needed.
            NotificationChannel channel = new NotificationChannel(channelId,
                    channelId,
                    NotificationManager.IMPORTANCE_DEFAULT);
//            AudioAttributes att = new AudioAttributes.Builder()
//                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
//                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
//                    .build();
            notificationBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
            channel.setSound(defaultSoundUri, audioAttributes);
            channel.setDescription(remoteMessage.getNotification().getBody());
            notificationManager.createNotificationChannel(channel);
            notificationManager.notify(0, notificationBuilder.build());
        }

0 个答案:

没有答案