即使 Android 应用程序被杀死或在后台运行,也通知非活动用户(7 天未打开应用程序) [英] Notify inactive users (who have not opened the app for 7 days) even if Android app gets killed or runs in background

查看:122
本文介绍了即使 Android 应用程序被杀死或在后台运行,也通知非活动用户(7 天未打开应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,需要通知非活动用户(7 天未打开应用程序).这意味着如果应用在后台运行 7 天或应用已被杀死且 7 天未使用,则必须发送通知.

I have a requirement where I need to notify inactive users (who have not opened the app for 7 days). This means the notification must be sent if the app is in background for 7 days or the app has been killed and not used for 7 days.

我使用类似于 this answerAlarmManager 遵循算法,但未发送通知如果应用程序被杀死.我试图集成 BroadcastReceiver 如下所示,但是当我从 CheckRecentRun 类的 OnDestroy() 调用它时它会创建一个无限循环(因为它会破坏本身定期).此外,正如我从 这个答案 中了解到的那样,当应用程序被杀死时,onDestroy() 并不总是被调用.

I followed an algorithm using AlarmManager similar to this answer but the notifications are not sent if the app gets killed. I tried to integrate BroadcastReceiver as shown below but it creates an infinite loop when I call it from OnDestroy() of CheckRecentRunclass (because it destroys itself periodically). Also, as I learnt from this answer, onDestroy() does not always get called when app is killed.

public class TimerRestarterBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TimerRestarterBroadcastReceiver.class.getSimpleName(), "Service has stopped");
    context.startService(new Intent(context, CheckRecentRun.class));
}
}

有没有更简单有效的方法来做到这一点?

Is there any simpler and effective way to do this?

推荐答案

使用 WorkManager 的可能场景:

Possible scenario using WorkManager:

  • 您创建了一个在 7 天内发送通知的任务
  • 每次用户使用应用时,将旧任务的时间更改为 7 天后的新任务

这篇关于即使 Android 应用程序被杀死或在后台运行,也通知非活动用户(7 天未打开应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆