服务中的通知不工作,android
我希望有一个永远运行的服务,每天下午6点发送通知,但对于某些reazon mi服务,几个小时后就会停止工作或发生错误,而我在下午6点没有任何通知 这是我的代码,我100%确定我的通知数据在那里,我使用本地数据库服务中的通知不工作,android,android,kotlin,service,Android,Kotlin,Service,我希望有一个永远运行的服务,每天下午6点发送通知,但对于某些reazon mi服务,几个小时后就会停止工作或发生错误,而我在下午6点没有任何通知 这是我的代码,我100%确定我的通知数据在那里,我使用本地数据库 class NotificationService : AppService() { private val timer: Timer = Timer(false) private var wakeLock: PowerManager.WakeLock? = null
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)
}
}
}
class NotificationService:AppService(){
专用val定时器:定时器=定时器(false)
private var wakeLock:PowerManager.wakeLock?=null
私有变量isServiceStarted=false
私有var通知=emptyArray()
私有lateinit变量notificationController:INotificationServiceController
private val calendar:calendar=calendar.getInstance().apply{
timeInMillis=System.currentTimeMillis()
设置(日历小时/天,18)
}
覆盖启动命令(intent:intent?,标志:Int,startId:Int):Int{
super.onStartCommand(intent、flags、startId)
notificationController=AdapterBuilder.notificationServiceController
startService()
返回起始点
}
覆盖乐趣onBind(意图:意图?):IBinder{
返回空
}
重写onDestroy(){
super.ondestory()
isServiceStarted=false
notificationController.setServiceState(ServiceStates.STOPPED.name)
notificationController.onDestroy()
timer.cancel()
}
私人娱乐checkDBTask(){
Log.d(“通知”、“检查数据库”)
notificationController.updateNotifications()
notificationController.getTodayNotifications{notifications->
this.notifications=notifications.toTypedArray()
val now=Calendar.getInstance()
if(now.get(Calendar.HOUR\u OF_DAY)==18&&now.get(Calendar.MINUTE)<1)sendNotifications()
}
}
私人娱乐startService(){
如果(isServiceStarted)返回
isServiceStarted=true
notificationController.setServiceState(ServiceStates.STARTED.name)
wakeLock=(作为PowerManager的getSystemService(Context.POWER\u服务)。运行{
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,“NotificationService::LOCK”)。应用{
获得(500)
}
}
GlobalScope.launch(Dispatchers.IO){
while(iServiceStarted){
发射(Dispatchers.IO){
checkDBTask()
}
延时(1*60*1000)
}
Log.d(“通知”,“通知服务已停止”)
}
}
私人娱乐通知(){
val alarmSound=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知)
val resultint=Intent(这是MainActivity::class.java)
resultint.flags=Intent.FLAG\u ACTIVITY\u NEW\u任务或Intent.FLAG\u ACTIVITY\u SINGLE\u TOP
resultent.putExtra(“数据”,“来自通知”)
val resultpendingent:pendingent?=pendingent.getActivity(
这
0,
结果,
PendingEvent.FLAG_更新_当前
)
notifications.forEach{notificationData->
val notification=NotificationCompat.Builder(此为NotificationsValues.CHANEL_ID)
.setContentIntent(结果结束内容)
.setSmallIcon(R.drawable.safin_图标)
.setContentTitle(notificationData.title)
.setContentText(notificationData.description)
塞斯特尔先生(
NotificationCompat.BigTextStyle()
.bigText(notificationData.description))
.setPriority(通知兼容优先级高)
.setSound(警报声)
.setAutoCancel(真)
.build()
NotificationManagerCompat.from(this).notify(notificationData.movementId?:0,通知)
}
}
}
我以前用过报警管理器,但没用。所以我试着这样做。android 8.0中的服务可能会被终止,有时不会再次启动,
WorkManager
是这类任务的合适类。因此,我将通知服务更改为WorkManager
这本指南对我帮助很大。如果应用程序是开放的,那么它就可以工作。服务和工作经理之间的区别是什么?不。工作经理在最新的android版本上工作。它还向后兼容到api级别14的早期版本。对于上一个问题的答案,后台服务不再适用于较新的android版本。您必须使用或WorkManager进行后台工作。请注意可以停止WorkManager的系统,具体取决于设备构造函数如何实现操作系统。