【问题标题】:How to set custom notification sound when App in background/not running?应用程序在后台/未运行时如何设置自定义通知声音?
【发布时间】:2021-06-12 13:04:54
【问题描述】:

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

Android.Net.Uri notification = Android.Net.Uri.Parse("android.resource://" + Application.Context.PackageName + "/raw/thesoundfile");                       
Ringtone rt = RingtoneManager.GetRingtone(a, notification);
rt.Play();

但是。显然,当 App 在后台时,这不会被执行,并且当通知到达时,操作系统仍然会播放(默认)通知声音。

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

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

【问题讨论】:

    标签: android notifications android-notifications


    【解决方案1】:

    首先将资源 (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);
    }
    

    【讨论】:

    • 谢谢。现在我的 Post-Oreo 工作正常,但是你能澄清一下是什么触发了 pre-oreo 示例的两个代码块吗?你能解释一下我如何让“Pre-Oreo”版本的 Android 与我的应用程序一起工作,以便它们在应用程序在后台时播放自定义声音吗?
    • 我假设这段代码通常会在 Activitiy 的 OnCreate() 中运行。根据收到的通知类型,如何使用上述方法选择性地播放不同的声音文件。我假设因为没有代码可以实际执行后台警报通知,所以存在一个限制,即只能播放一个声音文件,正如上述代码初始化时所定义的那样?
    • 当应用程序在后台时,任何 cmet 是否可以播放多个自定义声音,pre Oreo。即根据通知内容播放不同的声音,就像使用多个频道发布奥利奥一样?
    • pre-oreo 代码的最后一行不是真的触发了本地通知吗?据推测,当我们为稍后远程通知实际到达并且我们在后台进行初始化时,我们不应该这样做。
    • 另请注意 NotificationCompat 已“过时且已弃用”:-(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多