Android 当应用程序处于后台/未运行时,如何设置自定义通知声音?

Android 当应用程序处于后台/未运行时,如何设置自定义通知声音?,android,notifications,android-notifications,Android,Notifications,Android Notifications,当应用程序位于前台时,可以播放自定义通知声音,方法是在resources/raw文件夹中提供声音文件,然后在响应通知时,按照以下方式执行操作: Android.Net.Uri notification = Android.Net.Uri.Parse("android.resource://" + Application.Context.PackageName + "/raw/thesoundfile"); Ri

当应用程序位于前台时,可以播放自定义通知声音,方法是在resources/raw文件夹中提供声音文件,然后在响应通知时,按照以下方式执行操作:

Android.Net.Uri notification = Android.Net.Uri.Parse("android.resource://" + Application.Context.PackageName + "/raw/thesoundfile");                       
Ringtone rt = RingtoneManager.GetRingtone(a, notification);
rt.Play();
不过。显然,当应用程序在后台时,这不会执行,并且当通知到达时,操作系统仍将播放(默认)通知声音

如何以编程方式将此“后台”通知声音设置为与在前台播放的自定义通知声音相同


我查看了用于关闭此帖子的“副本”。这个问题是在7年前提出的,当时Android仍然是Jelly Bean版本(4.1-4.3.1)。7年前有效的方法在今天并不总是有效的。这个答案已经过时,无法在Oreo(10)版本上使用。

首先将资源(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_版本并使用NotificationChannel的setSound方法

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


//for pre-oreo mobiles
{
 NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.app_name))    .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity_.class), 0))
            .setContentText("temporary text")
            .setAutoCancel(true)
           .setSound(Uri.parse("android.resource://"
                            + context.getPackageName() + "/"
                            + R.raw.alert))
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_stat_notification);

    Notification notification = builder.build();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, notification);
}

谢谢你。我现在让Post-Oreo工作正常,但是你能澄清是什么触发了pre-Oreo示例的两段代码吗?你能解释一下我如何让Android的“Pre-Oreo”版本与我的应用程序一起工作,以便它们在应用程序处于后台时播放自定义声音吗?我假设此代码通常会在Activitiy的OnCreate()中运行。如何使用上述方法根据收到的通知类型有选择地播放不同的声音文件。我假设,由于没有任何代码可以实际执行后台警报通知,因此有一个限制,即只有一个声音文件可以播放,正如在初始化上述代码时所定义的那样?请对应用程序在后台时是否可以播放多个自定义声音(pre-Oreo)发表意见。i、 e.根据通知内容播放不同的声音,就像使用多个频道播放oreo后一样?oreo前代码的最后一行实际上不是发出本地通知吗?我们可能不应该这样做,当我们为以后的远程通知实际到达并且我们在后台时进行初始化时。还要注意NotificationCompat是“过时和不推荐的”:-(