简体   繁体   English

Android 静音模式 - 有没有办法让通知发出声音?

[英]Android silent mode - is there a way for a notification to sound?

Context语境

I am working on an app that uses FCM.我正在开发一个使用 FCM 的应用程序。 The use of this application is to alert a user of an event that is occurring (such as an alarm system).此应用程序的用途是提醒用户正在发生的事件(例如警报系统)。 In view of the alarm nature of the notification , it is essential that a sound is played when receiving a notification even if the smartphone is in silent or vibrate mode .鉴于通知警报性质,即使智能手机处于静音振动模式,接收通知时也必须播放声音。


Question

Is there a way to achieve this described behavior for all smartphone modes (silent, vibrate, sound) ?有没有办法为所有智能手机模式(静音、振动、声音)实现这种描述的行为?


What I've tried我试过的

  • As I am working with API26> I created a notification channel to have the highest priority which is Max Priority ,当我使用 API26> 时,我创建了一个具有最高优先级的通知通道,即Max Priority

  • I've set the notification channel to bypass Do Not Disturb mode like so:我已将通知渠道设置为绕过“请勿打扰”模式,如下所示:

    notificationChannel.SetBypassDnd(true);

    Obviously it only affects the Do Not Disturb mode and absolutely not what I want,显然它只影响请勿打扰模式,绝对不是我想要的,

  • In the notification builder, I've set the notification priority to Max and the category to Alarm :在通知构建器中,我将通知优先级设置为Max并将类别设置为Alarm

     .SetPriority(NotificationCompat.PriorityMax) .SetCategory(NotificationCompat.CategoryAlarm);

    Reading the Android documentation, this feature is also related to Do Not Disturb Mode.阅读Android文档,此功能也与请勿打扰模式有关。

I am actively looking for a solution to this problem, but at this point I'm a bit stuck.我正在积极寻找这个问题的解决方案,但在这一点上我有点卡住了。


Any suggestions ?有什么建议 ?

I've read about a full screen intent in the Android documentation but it's not written that a sound will fire if the smartphone is in silent mode.我已经在 Android 文档中阅读了有关全屏意图的内容,但没有写到如果智能手机处于静音模式会发出声音。

Maybe there is a way to create a service that rings when the notification arrives?也许有一种方法可以创建一个在通知到达时响铃的服务? But this service has to be running all the time, which isn't really a good design idea.但是这个服务必须一直运行,这不是一个很好的设计理念。

If you guys have any idea, any remarks or suggestions, i'd be grateful to read them !如果你们有任何想法,任何评论或建议,我将不胜感激阅读它们!

I believe you need to set priority for your notification.我相信您需要为您的通知设置优先级。

    private fun setPriorityForAlarmNotification() {
    if (notificationManager.isNotificationPolicyAccessGranted) {
      notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        val policy = NotificationManager.Policy(PRIORITY_CATEGORY_ALARMS, 0, 0)
        notificationManager.notificationPolicy = policy
      }
    }
  }

As I can see you setCategory for your notification builder is NotificationCompat.CategoryAlarm.正如我所看到的,您的通知生成器的 setCategory 是 NotificationCompat.CategoryAlarm。

However, in order to set this priority, you need this permission on your manifest但是,为了设置此优先级,您需要在清单上获得此权限

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" /> 

And request permission if needed并在需要时请求许可

    fun requestNotificationPolicyPermission() {
    val notificationManager = activity!!.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    
    if (!notificationManager.isNotificationPolicyAccessGranted) {
      val intent = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
      startActivityForResult(intent, REQUEST_CODE_NOTIFICATION_POLICY)
    }
  }

This solution should work absolutely.这个解决方案应该绝对有效。 Hope this can help you :D希望这可以帮助你:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM