【问题标题】:Schedule periodic local notification based on specific dates根据特定日期安排定期本地通知
【发布时间】:2021-05-31 16:04:57
【问题描述】:

我想让一个定期工作人员根据客户收集日期的总数安排通知。 我已经分开了,但我面临一个问题。

目标:
检查是否有明天取货日期的客户,然后安排在明天上午 10 点显示通知以提醒用户,并每天重复同样的事情。

我所做的工作:
首先,我基于定期工作管理器研究了这个解决方案。

CollectionNotificationWorker.java:

public class CollectionNotificationWorker extends Worker {

    public static final String WORKER_TAG = "COLLECTION_NOTIFICATION_WORKER_TAG";
    private final Context context;


    public CollectionNotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
        this.context = context;

    }

    @NonNull
    @Override
    public Result doWork() {

        checkTomorrowsCollection(
                //tomorrow's date 
        );

        return Result.success();
    }

    private void checkTomorrowsCollection(String date) {

        DbOperationCaller.read(AppDatabase.getAppDatabase().getCustomerDao()
                        .countCollectionByDate("%" + date + "%"),
                new GenericOnceTimeReadingDbOperationObserver<Integer>() {
                    @Override
                    public void onSuccess(@NotNull Integer customersWithCollectionDate) {

                        if (customersWithCollectionDate > 0) {
                            setAlarm();
                        }
                    }
                }
        );
    }

    private void setAlarm() {
        AlarmManager am = (AlarmManager) SharedLibrary.getApplicationContext()
                .getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(getApplicationContext(), AlarmBroadcast.class);

        PendingIntent pendingIntent =
                PendingIntent.getBroadcast(
                        context,
                        ALARM_REQUEST_CODE,
                        intent,
                        PendingIntent.FLAG_ONE_SHOT);

        am.set(AlarmManager.RTC_WAKEUP,
                    //tomorrow's date , pendingIntent);


    }

AlarmBroadcast.java:


public class AlarmBroadcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        DbOperationCaller.read(AppDatabase.getAppDatabase().getCustomerDao()
                        .countCollectionByDate(
                                "%" + convertToString(getCurrentCalendar().getTime()) + "%"
                        ),
                new GenericOnceTimeReadingDbOperationObserver<Integer>() {
                    @Override
                    public void onSuccess(@NotNull Integer customersWithCollectionDate) {
                        if (customersWithCollectionDate > 0) {
                            setupNotification(context, customersWithCollectionDate);
                        }
                    }
                }
        );
    }

    private void setupNotification(Context context, int customersWithCollectionDate) {
        // setup Notification here ...
    }
}

MainActivity.java:

PeriodicWorkRequest collectionCheckerWorkRequest = new PeriodicWorkRequest
                .Builder(CollectionNotificationWorker.class, 15, TimeUnit.MINUTES)
                .build();

        WorkManager.getInstance(this)
                .enqueue(collectionCheckerWorkRequest);

问题:
通知在某些情况下不显示,并且在 android 11+ 设备中,它会在我打开应用程序后显示。

【问题讨论】:

    标签: java android android-jetpack android-workmanager


    【解决方案1】:

    从我可以判断的情况来看,您的 WorkManager 很可能被关闭,并且一旦您重新打开应用程序,计划任务就会运行。

    这是 WorkManager 的一个已知问题,它实际上不是 WorkManager 问题,而是操作系统问题。 WorkManager 的行为如下:

    任务管理器关闭: 工作继续(稍后)

    重启设备(工作运行): 重启完成后继续工作

    应用信息“强制停止”: 工作停止,只有在应用重新启动时才会继续

    重启设备(工作被“强制停止”): 应用程序重新启动之前,工作不会继续

    这个问题是一些设备从最近的菜单中执行终止应用程序作为强制停止。默认情况下,大多数流行的应用程序都被列入操作系统的白名单。可能有一种解决方法,可以让用户将您的应用列入白名单,尽管我认为这不合适。

    您可以进一步讨论herehere too 以获得更多想法。

    【讨论】:

    • 好吧,如果 workmanager 对于这类用例效率不高,那么在不让 android 杀死进程的情况下设置定期后台工作的最佳解决方案是什么?谢谢你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    相关资源
    最近更新 更多