49

我将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);
}
4

6 回答 6

117
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;

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

于 2012-12-07T09:36:14.330 回答
18

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

notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
        + "://" + getPackageName() + "/raw/kalimba");
于 2012-12-09T04:10:22.633 回答
4

尝试这个:

Uri sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/notifysnd);
notification.setSound(sound);
于 2016-05-12T09:29:17.187 回答
3

你应该替换这一行:

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");
于 2018-03-05T15:06:32.610 回答
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"); 或片段 中的任何 ActivityHelperClass.givenotification(getActivity(),"Your Notification Message");

希望这对某人有所帮助。

于 2019-06-23T18:52:24.957 回答
-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());
        }

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

于 2019-03-18T07:35:04.583 回答