2

In Android O, I have the following NotificationChannel

private static String getNotificationChannelId() {
    WeNoteApplication weNoteApplication = WeNoteApplication.instance();

    final String default_notification_channel_id = weNoteApplication.getString(R.string.default_notification_channel_id);

    return default_notification_channel_id;
}

public static String createNotificationChannel() {
    final String default_notification_channel_id = getNotificationChannelId();

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        return default_notification_channel_id;
    }

    WeNoteApplication weNoteApplication = WeNoteApplication.instance();

    NotificationManager notificationManager =
            (NotificationManager) weNoteApplication.getSystemService(Context.NOTIFICATION_SERVICE);

    final String default_notification_channel_name = weNoteApplication.getString(R.string.default_notification_channel_name);
    final String default_notification_channel_description = weNoteApplication.getString(R.string.default_notification_channel_description);

    NotificationChannel notificationChannel = new NotificationChannel(
            default_notification_channel_id,
            default_notification_channel_name,
            NotificationManager.IMPORTANCE_HIGH
    );

    notificationChannel.setDescription(default_notification_channel_description);
    notificationChannel.enableLights(true);
    notificationChannel.enableVibration(true);

    notificationManager.createNotificationChannel(notificationChannel);

    // How to get the name of sound?
    AudioAttributes audioAttributes = notificationChannel.getAudioAttributes();
    android.util.Log.i("CHEOK", "toString--> " + audioAttributes.toString());
    android.util.Log.i("CHEOK", "getSound--> " + notificationChannel.getSound());
    android.util.Log.i("CHEOK", "getDescription--> " + notificationChannel.getDescription());

    return default_notification_channel_id;
}

I was wondering, is it possible to get the name of sound from NotificationChannel, as shown in screenshot?

enter image description here

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

1 Answers1

1

This is what I done. Get the sound uri from notification channel, and parse it with RingtoneManager.

NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = notificationManager.getNotificationChannel("channel_id");
Uri ringtoneUri = notificationChannel.getSound();
Ringtone r = RingtoneManager.getRingtone(mContext, ringtoneUri);
String nameOfSound = r.getTitle(mContext);
neobie
  • 2,847
  • 5
  • 27
  • 31