如何在安卓设备上设置自定义音效的通知?

50

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

这是我创建通知的方式:

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);
}

你的代码看起来没问题。尝试重新启动模拟器或手机。 - Aleksander Gralak
6个回答

118
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
如果定义了DEFAULT_SOUND,则默认声音将覆盖任何声音。

如何在通知中添加 .mp3 文件。 - Tushar Pandey
将您的 MP3 文件放入 raw 文件夹中。 - QuokMoon
2
如果我不写“notification.defaults”,那么播放自定义声音的可能性是什么? - JK Patel
1
我不明白这怎么回答了问题,为什么指定默认的灯光和振动会让声音工作? - PHPirate
1
有人尝试在Android 8上使用过吗?这个方法对我已经不起作用了! - Tano
可以从存储和扩展文件设置通知声音吗? - Pranav

18

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

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

5

试试这个:

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

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");

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,"您的通知消息"); 或者在片段中 HelperClass.givenotification(getActivity(),"您的通知消息");

希望这能帮助到某些人。


-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());
        }

希望这能帮到你。谢谢...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接