Android notification setSound is not working

后端 未结 8 1616
佛祖请我去吃肉
佛祖请我去吃肉 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 05:00

    You are accessing the sound in a subfolder in the resources

    change the source of your uri to

    Uri uri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.siren);
    

    For the default sound, use:

    notification.defaults |= Notification.DEFAULT_SOUND;
    
    0 讨论(0)
  • 2021-02-01 05:02

    Use this for setting sound

    Uri defaultSoundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + mContext.getPackageName() + "/raw/mysound");
    
    
    NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(mContext)
                            .setContentIntent(mainPIntent)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
                            .setContentTitle("" + title)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setContentText("" + body);
    
            NotificationManager mNotificationManager =
                    (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(title, NOTIFICATION_ID, mBuilder.build());
    
    0 讨论(0)
提交回复
热议问题