gpt4 book ai didi

android - 应用程序在后台/未运行时如何设置自定义通知声音?

转载 作者:行者123 更新时间:2023-12-04 01:37:24 26 4
gpt4 key购买 nike

可以在应用程序处于前台时播放自定义通知声音,方法是在资源/原始文件夹中提供声音文件,然后在对通知使用react时,执行以下操作:

Android.Net.Uri notification = Android.Net.Uri.Parse("android.resource://" + Application.Context.PackageName + "/raw/thesoundfile");                       
Ringtone rt = RingtoneManager.GetRingtone(a, notification);
rt.Play();
然而。显然,当应用程序在后台时,这不会被执行,当通知到达时,操作系统仍然会播放(默认)通知声音。
如何以编程方式将此“背景”通知声音设置为与在前台播放的自定义声音相同?
我查看了“重复” question那是用来关闭这篇文章的。这个问题是在 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_VERSION 并使用 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);
}

关于android - 应用程序在后台/未运行时如何设置自定义通知声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59029016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com