1

有人知道启用静音模式后如何在设备上接收通知吗?我正在使用 Web 服务器和 Firebase 发送通知推送,当 android 设备处于静音模式(请勿打扰) 时,似乎无法收到通知。但是,当我将智能手机置于静音模式时,我的闹钟仍在响起。然后我们应该能够收到分类为“警报”的通知,即使在 Silence 模式中也是如此。不?

4

1 回答 1

0

在静默模式下显示通知的最佳方式是显示全屏意图

 val summaryNotification =
        NotificationCompat.Builder(
            context,
            notificationChannelId
        )
            .setSmallIcon(getSmallIconForNotification())
            .setLargeIcon(largeIcon(context))
            .setStyle(bigPicStyle)
            .setContentTitle(title)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .addAction(
                R.drawable.ic_cancel_small,
                context.getString(R.string.dismiss),
                dismissIntent
            )
            .setAutoCancel(true)
            .setTimeoutAfter(durationInMilliSeconds!!.toLong())
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentIntent(
                contentIntent(
                    context,
                    SUMMARY_NOTIFICATION_ID
                )
            )
    if (!showFull) {
        summaryNotification.setSound(sound, AudioManager.STREAM_ALARM)
    }

    if (showFull) {
        summaryNotification.setFullScreenIntent(buildPendingIntent(context, i), true)
    }

    notificationManager.notify(
        SUMMARY_NOTIFICATION_ID,
        summaryNotification.build()
    )

建立待定意图:

  private fun buildPendingIntent(
    context: Context,
    intent: Intent
): PendingIntent? {
    return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}

当您从 firebase 接收数据时,检查手机是否处于活动状态,然后发送适当的通知

 val mPowerManager = context!!.getSystemService(Context.POWER_SERVICE) as PowerManager
            if (!mPowerManager.isInteractive) {
                showFullNotification(true)
            }else{
                showFullNotification(false)
            }

那么你必须创建一个令人震惊的活动。

在警报活动中隐藏操作栏:

 (this as AppCompatActivity?)!!.supportActionBar!!.hide()

并在清单中

<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
于 2021-08-02T10:38:05.007 回答