Oreo +通知声音问题

时间:2018-10-17 08:08:24

标签: android push-notification firebase-cloud-messaging android-8.0-oreo

我正在尝试为通知设置自定义声音。问题在于它总是在res / raw文件夹中播放第一声音,无论其中有多少文件或我试图更改uri的数量。如果我从原始文件夹中删除所有文件,则根本不会播放声音。在Android 6上,效果很好。

我希望能够设置来自外部/内部存储的声音以及系统声音。有可能吗?

这是我的代码:

notificationSoundUri = GeneralSettingsManager.getSoundForNotification(getApplicationContext(), site, pushType);
                mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
                Intent notificationIntent = new Intent(this, Splash.class);
                notificationIntent.setAction(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
                int smallIcon = R.drawable.big_green_v;
                int backgroundColor = 0x8D1919;
                String channelId = "default2";
                NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(this, channelId)
                                .setSmallIcon(smallIcon)
                                .setColor(backgroundColor)
                                .setContentTitle(title)
                                .setContentText(message)
                                .setAutoCancel(true)
                                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                                .setDefaults(Notification.DEFAULT_VIBRATE);


                if(TextUtils.isEmpty(notificationSoundUri))
                {
                    Timber.e("NOTIFICATION SOUND * MISSING");
                    mBuilder.setVibrate(new long[]{0L});
                }
                else
                {
                    Timber.e("NOTIFICATION SOUND * " + notificationSoundUri);
                    mBuilder.setSound(Uri.parse(notificationSoundUri));
                }

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

                    if(!TextUtils.isEmpty(notificationSoundUri))
                    {
                        // Create an Audio Attribute
                        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                                .build();

                        //remove old channel
                        try {
                            mNotificationManager.deleteNotificationChannel(channelId);
                        }
                        catch (Exception e)
                        {
                            //do nothing
                        }


                        // Create new channel
                        NotificationChannel notificationChannel = new NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_DEFAULT);
                        notificationChannel.setSound(Uri.parse(notificationSoundUri), audioAttributes);
                      mNotificationManager.createNotificationChannel(notificationChannel);
                    }
                }

                PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                mBuilder.setContentIntent(contentIntent);

                mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

编辑:  上面的代码来自FirebaseMessagingService。用户将从外部存储或系统声音列表中选择所需的通知声音,并在显示通知时播放该声音。

1 个答案:

答案 0 :(得分:0)

是的,您可以!您必须使用RingtoneManager(用于系统声音),

Uri ringSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.sound = ringSound;

对于外部/内部 您需要为特定的声音文件找到uri。

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, 
RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
this.startActivityForResult(intent, 2);

处理响应

 @Override
 protected void onActivityResult(final int requestCode, final int resultCode, 
 final Intent intent)
 {
   if (resultCode == Activity.RESULT_OK && requestCode == 2)
   {
      Uri uri = Intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

      if (uri != null)
      {
          this.chosenRingtone = uri.toString();
      }
      else
      {
          this.chosenRingtone = null;
      }
  }            

}