Android notification setSound is not working

后端 未结 8 1745
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 04:46

In my hybrid Cordova Android app targeting API 23+ I want to use a custom sound for notifications. To that end I have done the following

  • In plugin.xml
8条回答
  •  我在风中等你
    2021-02-01 04:55

    I am not sure but i think issue is that you are doing the wrong way "/raw/mysound.mp3 :

    Uri uri = Uri.parse("android.resource://" + ctxt.getPackageName() + "/raw/mysound.mp3");
    

    First add the permission in manifest : uses-permission android:name="android.permission.VIBRATE" /> then you can set the default sound like :-

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    mBuilder.setSound(alarmSound);
    

    and for vibration:

    mBuilder.setVibrate(new long[] { 1000, 1000});
    

    for custom sound, put mp3 file on this path:Res\raw\sound.mp3 and then

    Notification notification = builder.build();
     notification.sound = Uri.parse("android.resource://"
            + context.getPackageName() + "/" + R.raw.sound);
    

提交回复
热议问题