【问题标题】:Android silent mode - is there a way for a notification to sound?Android静音模式 - 有没有办法发出通知?
【发布时间】:2021-08-02 18:01:09
【问题描述】:

上下文

我正在开发一个使用 FCM 的应用。此应用程序的用途是提醒用户正在发生的事件(例如警报系统)。鉴于通知的警报性质,即使智能手机处于静音状态在收到通知时播放声音也很重要 /em> 或 振动 模式


问题

有没有办法为所有智能手机模式实现上述行为(静音、振动、声音)


我尝试过的

  • 当我使用 API26> 时,我创建了一个具有最高优先级的通知通道,即 Max Priority

  • 我已将通知通道设置为绕过请勿打扰模式,如下所示:

    notificationChannel.SetBypassDnd(true);

    显然它只影响请勿打扰模式,绝对不是我想要的,

  • 在通知生成器中,我将通知优先级设置为 Max,将类别设置为 Alarm

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

    阅读Android文档,此功能也与请勿打扰模式有关。

我正在积极寻找解决此问题的方法,但此时我有点卡住了。


有什么建议吗?

我在 Android 文档中读到了 full screen intent,但没有写到如果智能手机处于静音模式会发出声音。

也许有一种方法可以创建一个在通知到达时响铃的服务?但是这个服务必须一直运行,这并不是一个好的设计理念。

如果你们有任何想法、意见或建议,我将不胜感激!

【问题讨论】:

标签: android push-notification alarm


【解决方案1】:

我认为您需要为通知设置优先级。

    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
      }
    }
  }

据我所知,您为通知构建器设置的类别是 NotificationCompat.CategoryAlarm。

但是,为了设置此优先级,您需要在清单上获得此权限

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

并在需要时请求许可

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

这个解决方案应该绝对有效。希望这可以帮助你:D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 2023-03-13
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    相关资源
    最近更新 更多