0

I am trying to get Uri on one of system ringtone(user chouse it by himself). and then this Uri is using in notification. But notification plays default sound instead of chosed by user. This is code for button which opens activity for choosing sound

ringtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
                startActivityForResult(intent, 5);

after this i am getting URI

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 5){
            uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            ringtone.setText(uri.toString());
        }
    }

for example, Uri after choosing looks like this: CONTENT://MEDIA/INTERNAL/AUDIO/MEDIA/36 is it correct Uri?

and by clicking another button, notification should appear


 music.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
                Resources res = context.getResources();
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
                builder.setContentIntent(contentIntent).setSmallIcon(R.drawable.play25x25).setContentTitle("Title").setContentText("Text").setSound(uri).setChannelId("1");
                NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);;
                NotificationChannel channel = new NotificationChannel("1", "name", NotificationManager.IMPORTANCE_DEFAULT);
                notificationManager.createNotificationChannel(channel);
                notificationManager.notify(1, builder.build());
            }
        });

aaand it appears, but with default system notification sound, not with installed by user Please help. What i am doing wrong? I thought, maybe i need any permissions in manifest?

1 Answers1

0

Do you create your notification channel when app starts? In App's onCreate(). Like this

 channelId = getString(R.string.notification_chat_channel);
        channel = new NotificationChannel(channelId,
                "Some channel name",
                NotificationManager.IMPORTANCE_DEFAULT);
        Uri uri = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.push_sound);
        channel.setSound(uri, att);
        notificationManager.createNotificationChannel(channel);
Evgeniy
  • 509
  • 5
  • 14