PeriodicWorkRequest无法正常工作-为什么?

时间:2019-09-25 12:44:18

标签: android performance firebase-cloud-messaging android-workmanager

我正在使用带有Workmanger的PeriodicWorkRequest从应用程序每2小时发送一次本地通知。但是什么也没发生。一旦我打开应用程序,doWork()方法就会被调用并显示通知,但是当应用程序在后台或从最近的应用程序历史记录中删除时,通知不会出现

 Constraints constraints = new Constraints.Builder()
            .setRequiresCharging(false)
            .setRequiresDeviceIdle(true)
            .setRequiresBatteryNotLow(true)
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .build();


    final PeriodicWorkRequest periodicWorkRequest
            = new PeriodicWorkRequest.Builder(ShowNotificationJob.class, 2, TimeUnit.HOURS)
            .setConstraints(constraints)
            .build();

    WorkManager.getInstance(this).enqueueUniquePeriodicWork(TASK_ID,
            ExistingPeriodicWorkPolicy.REPLACE, periodicWorkRequest);

1 个答案:

答案 0 :(得分:-1)

确保使用稳定版本的Workmanager Library https://developer.android.com/jetpack/androidx/releases/work#declaring_dependencies

如果不需要,请删除这些 private static String UPDATE_STATUS_OF_IMPORT_SESSION = "\n UPDATE kddb.import_sessions SET running_status = ? \n" + " WHERE username = ? AND deployment_type_id = ? AND \n" + " filename = ?; "; public void updateStatusOfImport_session(Boolean running_status, String username, Integer deployment_type_id, String filename) { logger.info(UPDATE_STATUS_OF_IMPORT_SESSION); jdbcTemplate.update(UPDATE_STATUS_OF_IMPORT_SESSION, running_status, username, deployment_type_id, filename); logger.info("Successfully update a status (set {}) of record with parameters: username={} deployment_type_id={} filename={}", running_status, username, deployment_type_id, filename); }

https://developer.android.com/reference/androidx/work/Constraints.Builder

  

使用这些约束可能是导致无法在后台运行的原因   因此请确保将这些约束用作要求

     

并使用ExistingPeriodicWorkPolicy.KEEP而不是ExistingPeriodicWorkPolicy.REPLACE-如果工作管理器不会创建新对象

constraints

最终密码

.setRequiresCharging(false)
                .setRequiresDeviceIdle(true)
                .setRequiresBatteryNotLow(true)
                .setRequiredNetworkType(NetworkType.CONNECTED)