androidnotificationsandroid-8.0-oreonotification-channel

How to getSound(), getName() from application NotificationChannel >=Oreo


How to get following details from particular application Notification channel

I have tried with following code but it returns system default channel details

 Uri=playSound ;
 String id = ApplicationClass.getInstance().HighNotificationChannelID;
 NotificationChannel mChannels = new NotificationChannel(id, getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
 playSound = mChannels.getSound();

O/P:- content://settings/system/notification_sound


Solution

  • After many searches I found the following way from which you can get all the details.

    Following link shows How to Open your app notification channel setting programmatically.

    open app notification setting

    This may help someone.

    Following code will return you all channels of application

    public static List<NotificationChannel> getNotificationChannels(Context context) {
        if (isNotificationChannelSupported(context)) {
            NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager == null) {
                Log.e("", "Notification manager is null");
                return null;
            }
            return notificationManager.getNotificationChannels();
        }
        return null;
    }
    
    public static boolean isNotificationChannelSupported(Context context) {
        return Build.VERSION.SDK_INT >= 26 && getTargetSdkVersion(context) >= 26;
    }
    
    private static int getTargetSdkVersion(Context context) {
        int targetSdk = -1;
        if (context != null) {
            targetSdk = context.getApplicationInfo().targetSdkVersion;
        }
        return targetSdk;
    }
    

    Following line will return you sound Uri same way you can get name,Id etc. get(2) is channel you want to refer to retrieve details you can use 0,1 also

    Uri soundUri= getNotificationChannels(this).get(2).getSound()