我想从资源文件夹,Android设置推送通知声音

时间:2017-04-28 05:19:46

标签: android push-notification assets

我在资产文件夹中有一个名为“error.mp3”的.mp3文件。我希望设置该声音以及推送通知,我该怎么做,我已经尝试过这个代码来做那个

 Uri sound = Uri.parse("file:///android_asset/error.mp3");
 mBuilder.setSound(sound);

但它不起作用,当我使用默认通知声音时,它正在工作。我需要一些帮助...

这是我的 BroadcastReceiver

public class Notificationmassage extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {


    showNotification(context);

}

private void showNotification(Context context) {
    Log.i("notification", "visible");



    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic)
                    .setContentTitle("Radio Planet")
                    .setContentText("Explore music");
    mBuilder.setContentIntent(contentIntent);


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


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



   Uri sound = Uri.parse("file:///android_asset/error.mp3");
   mBuilder.setSound(sound);

    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(2, mBuilder.build());


}
}

3 个答案:

答案 0 :(得分:1)

请尝试将您的.mp3文件放在原始文件夹而不是assets,有时它不起作用,请使用此类

Uri path = Uri.parse("android.resource://com.example.test/" + R.raw.sample);

com.example.test //将此替换为您的包名

然后设置

mBuilder.setSound(path);

文件夹结构

res/raw/test.mp3

希望它对你有用。享受编程。

enter image description here

enter image description here

答案 1 :(得分:0)

请将您的.mp3文件放在raw目录中,然后尝试以下代码

Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.error);

答案 2 :(得分:0)

将其放入原始文件夹并使用如下

    <a href="#" 
      onClick="if(confirm('Are you sure your want to delete?')){alert('Delete');} else {alert('Dont delete')}">
     Delete</a>