setexactandallowwhileidle 在空闲模式下不触发通知。

huangapple go评论62阅读模式
英文:

setexactandallowwhileidle not trigger notification in idle mode

问题

我有一个问题,我正在尝试使用闹钟管理器触发通知。但是一旦设备进入空闲模式,通知就不会弹出,只有在进入应用程序后才会出现。

这是我的活动中的代码:

val intent = Intent(applicationContext, AlarmReceiver::class.java)
intent.putExtra(TITLE_EXTRA, "标题")
intent.putExtra(MESSAGE_EXTRA, "adb shell dumpsys deviceidle step")

val pendingIntent = PendingIntent.getBroadcast(
    applicationContext,
    NOTIFICATION_ID,
    intent,
    PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)

alarmManager.setExactAndAllowWhileIdle(
    AlarmManager.RTC_WAKEUP,
    System.currentTimeMillis() + 2 * 60000,
    pendingIntent
)

这是我的AlarmReceiver中的代码:

class AlarmReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val builder = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle(intent.getStringExtra(TITLE_EXTRA))
            .setContentText(intent.getStringExtra(MESSAGE_EXTRA))
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .build()

        val notificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(NOTIFICATION_ID, builder)
    }
}

我在清单文件中使用了以下权限:

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

我该怎么做才能在空闲/休眠模式下也弹出通知呢?

英文:

I have a problem, I'm trying to trigger a notification with alarm manager.
But as soon as the device enters idle mode,
The notification does not pop up for me, it only appears after entering the application

this is my code in activity:

val intent = Intent(applicationContext, AlarmReceiver::class.java)
    intent.putExtra(TITLE_EXTRA, &quot;title&quot;)
    intent.putExtra(MESSAGE_EXTRA, &quot;adb shell dumpsys deviceidle step\n&quot;)

    val pendingIntent = PendingIntent.getBroadcast(
        applicationContext,
        NOTIFICATION_ID,
        intent,
        PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    )

    alarmManager.setExactAndAllowWhileIdle(
        AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis() + 2 * 60000,
        pendingIntent
    )

this is my code alarmReciver:

class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
    val builder = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setContentTitle(intent.getStringExtra(TITLE_EXTRA))
        .setContentText(intent.getStringExtra(MESSAGE_EXTRA))
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .build()

    val notificationManager =
        context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.notify(NOTIFICATION_ID, builder)
}

}

and i use

&lt;uses-permission android:name=&quot;android.permission.SCHEDULE_EXACT_ALARM&quot; /&gt;

in menifest.

What can I do so that the notification pops up for me even in idle/doze mode?

答案1

得分: 1

自 Android 12 开始,您需要请求 SCHEDULE_EXACT_ALARM 的运行时权限,即使如此,也不能保证及时收到权限(但非常非常可能)

我也遇到了两个限制,您需要以某种方式传达给用户:

  • 用户需要打开应用的系统设置并禁用“如果未使用则暂停应用活动” - 如果用户在一段时间内不打开您的应用(只有前台活动计数),比如一个月,进程将被终止,任何进一步的闹钟都将不会被传递。
  • 进入应用的系统电池设置并将选项更改为“不受限制” - 这更多是为了允许更精确的传递,而不是之前的情况。

我的应用中也有相同的用例(在精确的时间显示通知),在大多数情况下可以正常工作,但仍然可能发生在系统处于深度休眠模式或设备正在运行低电量优化时延迟最多2小时的情况。

英文:

Since Android 12, you need to request runtime permission for SCHEDULE_EXACT_ALARM, and even then you are not guaranteed to receive it in time (but very very likely)

There are also two limitations that I came across that you need to communicate somehow to the user:

  • User need to open system settings for your app and disable "pause app activity if unused" - without that, if user will not open your app for some time (only foreground activity counts), like month, the process will be killed and any further alarm will not be delivered
  • enter system battery settings for your app and change option to "unrestricted" - this one is more to allow more exact delivery than the previous

I have the same usecase in my app (show notification at exact time) and it works in most cases, but it still can happen that it fires up to 2h later when system is in doze mode or device have low battery optimizations running

huangapple
  • 本文由 发表于 2023年6月22日 20:19:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定