android - Android workmanager 作业在应用程序关闭后不运行(三星 s9+)
问题描述
我正在尝试使用 WorkManager 运行一次性工作。我不知道为什么,但是如果我从最近的应用程序菜单中关闭我的应用程序,直到我启动应用程序才会开始:/。用三星 Galaxy s9+ 试过。安卓 9 派。任何帮助,将不胜感激。据我所知,即使手机重新启动,workmanager 也应该可以工作。
val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.NOT_REQUIRED).build()
val request = OneTimeWorkRequestBuilder<NotificationJob>()
.addTag(item.uniqueId)
.setInputData(data)
.setConstraints(constraints)
//TODO revert
// .setInitialDelay(warrantyItem.reminderDate - System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.setInitialDelay(1, TimeUnit.MINUTES)
.build()
WorkManager.getInstance(this@MainActivity).enqueue(request)
工作很简单。只是通知:
override fun doWork(): Result {
// Get the input
val uniqueId = inputData.getString(JOB_KEY)
if (uniqueId != null) {
sendNotification(uniqueId, "title", "subtitle")
}
return Result.success()
}
private fun sendNotification(uniqueId: String, title: String, subtitle: String) {
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK
intent.putExtra(NOTIFICATION_ID, id)
val notificationManager =
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val bitmap = applicationContext.vectorToBitmap(R.drawable.ic_warranty_icon)
val titleNotification = "$title"
val subtitleNotification = subtitle
val pendingIntent = getActivity(applicationContext, 0, intent, 0)
val notification = NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL)
.setLargeIcon(bitmap).setSmallIcon(R.drawable.ic_warranty_icon)
.setContentTitle(titleNotification).setContentText(subtitleNotification)
.setDefaults(DEFAULT_ALL).setContentIntent(pendingIntent).setAutoCancel(true)
notification.priority = PRIORITY_MAX
if (SDK_INT >= O) {
notification.setChannelId(NOTIFICATION_CHANNEL)
val ringtoneManager = getDefaultUri(TYPE_NOTIFICATION)
val audioAttributes = AudioAttributes.Builder().setUsage(USAGE_NOTIFICATION_RINGTONE)
.setContentType(CONTENT_TYPE_SONIFICATION).build()
val channel =
NotificationChannel(NOTIFICATION_CHANNEL, NOTIFICATION_NAME, IMPORTANCE_HIGH)
channel.enableLights(true)
channel.lightColor = RED
channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
channel.setSound(ringtoneManager, audioAttributes)
notificationManager.createNotificationChannel(channel)
}
with(NotificationManagerCompat.from(applicationContext)) {
// notificationId is a unique int for each notification that you must define
notify(uniqueId, ID, notification.build())
}
}
解决方案
推荐阅读
- python - 在 Matplotlib 中将所有时间刻度移动一定量?
- google-sheets - 谷歌表格中的多个搜索条件
- kubernetes-ingress - istio 将 http 路由到 https 上游原因导致 302
- python - 遍历数据框的有效方法
- tfs - Azure DevOps 服务器,无法更改工作项类型
- python - 透视数据框的想法:从长到宽
- php - 将 XAMPP 移动到另一个目录后的配置?
- javascript - 使 div 停留在 div 下方的某个位置,该位置以编程方式增加高度
- json - 从 html 标记中获取值并以字符串类型保存在 python 字典中
- reactjs - Primereact 树上下文菜单在操作后不隐藏