小米手机删除最近使用的应用程序后,杀死了我的多个应用程序通知,但前台服务只能保留一个。该怎么办?

发布于 2025-01-12 19:10:24 字数 1427 浏览 1 评论 0原文

我正在编写一个提醒应用程序,它具有以下用例:

  1. 用户为 A 小时设置提醒
  2. 。用户为 B 小时设置提醒。A
  3. 小时到达,弹出提醒通知。用户不会忽略它。
  4. B 小时到来,弹出提醒通知。此时状态栏上有两个通知。

到目前为止,我一直在使用:

notificationManager.notify(
    alarmId,
    Notification.Builder(context, Constants.PRIMARY_CHANNEL_ID)
       .setSmallIcon(R.drawable.idle_1)
       .setCustomContentView(remoteViews)
       .setOngoing(true)
       .setContentIntent(notifyPendingIntent)
       .setAutoCancel(false).build()
)

没有问题,但显然小米会在抛出通知的应用程序被删除时杀死通知从最近的事情来看。对于使用我的应用程序的所有小米设备来说,这将是一个严重错误;所以我尝试使用 ForegroundService 并应用这个

问题是使用 ForegroundService 不适用于处理多个通知:如果两个提醒启动服务,则最近的提醒通知将杀死并替换较早的通知。 (正如这个答案中所解释的)。你能看到我陷入的循环吗?

我的 ForegroundService 没什么大不了的,当我测试它时它工作得很好,但它有通知替换的东西。

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    super.onStartCommand(intent, flags, startId)
    // ...
    startForeground(alarmId, notification)
    return START_STICKY
}

目前我完全陷入困境:我是否错过了什么,或者我应该因此放弃小米手机所拥有的巨大市场份额?提前致谢!

I am writing a reminder app which has the following use case:

  1. The user sets up a reminder for hour A.
  2. The user sets up a reminder for hour B.
  3. Hour A arrives and the reminder notification pops up. The user does NOT dismiss it.
  4. Hour B arrives and the reminder notification pops up. Two notifications are on the status bar at that moment.

Until now, I was using:

notificationManager.notify(
    alarmId,
    Notification.Builder(context, Constants.PRIMARY_CHANNEL_ID)
       .setSmallIcon(R.drawable.idle_1)
       .setCustomContentView(remoteViews)
       .setOngoing(true)
       .setContentIntent(notifyPendingIntent)
       .setAutoCancel(false).build()
)

Without a problem, but apparently Xiaomi kills notifications when the app that threw them gets removed from recents. This would be a critical error for all Xiaomi devices using my app; so I tried using a ForegroundService and applying this.

The problem is using a ForegroundService is not intended for handling multiple notifications: if two reminders start the service, the most recent reminder notification will kill and replace the earlier one. (As explained in this answer). Can you see the loop I'm trapped in?

My ForegroundService is no big deal, it worked perfectly when I tested it but it has the notification replacement thing.

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    super.onStartCommand(intent, flags, startId)
    // ...
    startForeground(alarmId, notification)
    return START_STICKY
}

At the moment I'm completely stuck: am I missing something or should I just ditch the huge market share Xiaomi phones have due to this? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文