如何在android中设置自定义声音通知

问题描述 投票:45回答:6

我将mp3(kalimba.mp3)文件复制到raw文件夹中的res文件夹中。但是当触发通知时,它会产生默认声音。

这是我发出通知的方式:

protected void GenerateNotify() {

    NotificationManager myNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);  
    Notification notification=new Notification(android.R.drawable.ic_btn_speak_now,"hi",100);
    Intent intent=new Intent(getApplicationContext(),as.class);
    PendingIntent contentintent=PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);
    notification.setLatestEventInfo(getApplicationContext(), "Hi","date", contentintent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.sound = Uri.parse("android.resource://com.example.serviceproject/" + R.raw.kalimba);
    myNotificationManager.notify(NOTIFICATION_ID,notification);
}
android
6个回答
111
投票
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;

如果定义了DEFAULT_SOUND,则默认声音会覆盖任何声音


17
投票

R.raw.kalimba是一个整数资源ID;你想要那个Uri中声音资源的名称。所以尝试:

notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
        + "://" + getPackageName() + "/raw/kalimba");

4
投票

试试这个:

Uri sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/notifysnd);
notification.setSound(sound);

2
投票

你应该替换这一行:

notification.sound = Uri.parse("android.resource://com.example.serviceproject/" + R.raw.kalimba);

有了这个:

notification.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/kalimba"));

或者在某些情况下:

notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/kalimba");

0
投票

我在HelperClass.java中创建了这个静态函数

    public static void givenotification(Context context,String message) {
         NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

         Intent notificationIntent = new Intent(context, DesireActivity.class);
         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

         Notification notification = new Notification();
         NotificationCompat.Builder builder;
         builder = new NotificationCompat.Builder(context);

         notification = builder.setContentIntent(pendingIntent)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setTicker(context.getString(R.string.app_name)).setWhen(System.currentTimeMillis())
            .setAutoCancel(true).setContentTitle(context.getString(R.string.app_name))
            .setContentText(message).build();
         notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.kalimba);
         notificationManager.notify(1, notification);
     }

然后任何活动,如MainActivity HelperClass.givenotification(MainActivity.this,"Your Notification Message");或片段HelperClass.givenotification(getActivity(),"Your Notification Message");

希望这能有所帮助。


-1
投票

我已经实现了这种方式在我的应用程序中设置自定义通知声音。

  int notificationID=001;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

        mBuilder.setSmallIcon(R.drawable.room);
        mBuilder.setContentTitle("Room Rent , Pay Room Rent");
        mBuilder.setContentText("Hi, Its time to pay your Room Rent!");
        mBuilder.setAutoCancel(true);
        mBuilder.setColor(getResources().getColor(R.color.colorViolet));
        mBuilder.setSound(Uri.parse("android.resource://com.roommanagement.app/" + R.raw.notification_sound));

        if (mNotificationManager!=null){
            mNotificationManager.notify(notificationID, mBuilder.build());
        }

跳这会帮助你。谢谢......

© www.soinside.com 2019 - 2024. All rights reserved.