如何在安卓手机上添加自定义通知声音

21

我需要管理通知,因此需要在通知到来时管理自定义声音。你有任何想法如何实现吗?

我已经将声音文件复制到了我的raw文件夹中,所以有人有任何想法可以帮我将其实现到我的项目中吗?

提前致谢。


https://dev59.com/tGYr5IYBdhLWcg3wVYzJ - Kinnar Vasa
2个回答

47
首先,在“资源”(res)文件夹中创建名为“raw”的文件夹,并将文件(YOUR_SOUND_FILE.MP3)放入其中,然后使用下面的代码行进行自定义声音。
   NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context,
            SlidingMenuActivity.class);
    notificationIntent.putExtra("isInbox", true);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

使用以下代码进行自定义音效

 notification.sound =Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.FILE_NAME);//Here is FILE_NAME is the name of file that you want to play


    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);

编辑更新

对于安卓版本为Oreo及更高版本,您需要检查SDK_VERSION并使用NotificationChannelsetSound方法。

Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.FILE_NAME);  //Here is FILE_NAME is the name of file that you want to play

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel mChannel = new NotificationChannel("YOUR_CHANNEL_ID",
                "YOUR CHANNEL NAME",
                NotificationManager.IMPORTANCE_DEFAULT)

            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();

            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, 
                    context.getString(R.string.app_name),
                    NotificationManager.IMPORTANCE_HIGH);

            // Configure the notification channel.
            mChannel.setDescription(msg);
            mChannel.enableLights(true);
            mChannel.enableVibration(true);
            mChannel.setSound(sound, attributes); // This is IMPORTANT


            if (mNotificationManager != null)
                mNotificationManager.createNotificationChannel(mChannel);
        }

如何在7.0以上的系统中播放自定义通知声音?我已经尝试使用路径file://content://FILE PROVIDER,但无法播放自定义声音,只能播放系统声音。 - Sagar
需要从设备存储的文件中播放通知吗? - Ravindra Kushwaha
请问这个有任何更新吗? - Sagar
2
从我的角度来看,您不能播放外部文件以进行通知。您需要将其放在原始文件夹中。 - Ravindra Kushwaha
1
@Ravindra Kushwaha,谢谢。 - Alexey Bilousov
显示剩余7条评论

11
Intent i = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .setContentTitle("I want food")
    .setContentText(notificationcontent)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentIntent(pi)
    .setAutoCancel(true)
    .setDefaults(Notification.FLAG_ONLY_ALERT_ONCE);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
MediaPlayer mp= MediaPlayer.create(contexto, R.raw.your_sound);
mp.start();
manager.notify(73195, builder.build());

5
可以的。这个功能已经在运行了,但是默认的通知声音也被添加进去了。如何停止它? - Ssubrat Rrudra
我也想知道这个答案。 - markharrop
2
@SsubratRrudra - 你可能已经明白了。但对于其他人来说,setSound(null)就可以了。 - Alex
到目前为止,这是最好的答案。它轻松解决了我的问题,您不需要担心Android操作系统版本问题。 - MD. Shafiul Alam Biplob
这个解决方案在应用程序后台似乎无效。 - Binh Ho

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