[英]Notifications in service not working, android
我想要一個永遠運行並在每天下午 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 中的服務可以被kill掉,有的時候不會再啟動, WorkManager
就是class 適合這類任務的。 因此,我將通知服務更改為WorkManager
。
https://medium.com/androiddevelopers/workmanager-periodicity-ff35185ff006本指南對我幫助很大。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.