我想要一个永远运行并每天下午 6 点发送通知的服务,但由于某些原因,mi 服务停止工作或几个小时后发生错误,而我在下午 6 点没有任何通知。
这是我的代码,我 100% 确定我的通知数据在那里,我使用本地数据库
class NotificationService : AppService() {
private val timer: Timer = Timer(false)
private var wakeLock: PowerManager.WakeLock? = null
private var isServiceStarted = false
private var notifications = emptyArray<INotification>()
private lateinit var notificationController: INotificationServiceController
private val calendar: Calendar = Calendar.getInstance().apply {
timeInMillis = System.currentTimeMillis()
set(Calendar.HOUR_OF_DAY, 18)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
notificationController = adaptersBuilder.notificationServiceController
startService()
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onDestroy() {
super.onDestroy()
isServiceStarted = false
notificationController.setServiceState(ServiceStates.STOPPED.name)
notificationController.onDestroy()
timer.cancel()
}
private fun checkDBTask() {
Log.d("NOTIFICATION", "Checking db")
notificationController.updateNotifications()
notificationController.getTodayNotifications { notifications ->
this.notifications = notifications.toTypedArray()
val now = Calendar.getInstance()
if (now.get(Calendar.HOUR_OF_DAY) == 18 && now.get(Calendar.MINUTE) < 1) sendNotifications()
}
}
private fun startService() {
if (isServiceStarted) return
isServiceStarted = true
notificationController.setServiceState(ServiceStates.STARTED.name)
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NotificationService::lock").apply {
acquire(500)
}
}
GlobalScope.launch(Dispatchers.IO) {
while (isServiceStarted) {
launch(Dispatchers.IO) {
checkDBTask()
}
delay(1*60*1000)
}
Log.d("NOTIFICATION", "Notification service stopped")
}
}
private fun sendNotifications() {
val alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val resultIntent = Intent(this, MainActivity::class.java)
resultIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP
resultIntent.putExtra("data", "fromNotification")
val resultPendingIntent: PendingIntent? = PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
notifications.forEach { notificationData ->
val notification = NotificationCompat.Builder(this, NotificationsValues.CHANEL_ID)
.setContentIntent(resultPendingIntent)
.setSmallIcon(R.drawable.safin_icon)
.setContentTitle(notificationData.title)
.setContentText(notificationData.description)
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(notificationData.description))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(alarmSound)
.setAutoCancel(true)
.build()
NotificationManagerCompat.from(this).notify(notificationData.movementId ?: 0, notification)
}
}
}
我以前使用过警报管理器,但不起作用。所以我尝试这个。
android 8.0 中的服务可能会被终止,有时不会再次启动,
WorkManager
是适合此类任务的类。因此,我将通知服务更改为 WorkManager
。
https://medium.com/androiddevelopers/workmanager-periodicity-ff35185ff006本指南对我帮助很大。