Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当应用程序处于后台/被终止时触发服务通知_Android_Kotlin_Service_Notifications - Fatal编程技术网

Android 当应用程序处于后台/被终止时触发服务通知

Android 当应用程序处于后台/被终止时触发服务通知,android,kotlin,service,notifications,Android,Kotlin,Service,Notifications,我正在制作一个todolist应用程序,其中用户需要添加一个任务以及日期和时间,我稍后使用它来触发通知,当应用程序位于前台时,它工作正常,但由于android oreo之后服务会受到限制,现在我不知道如何在应用程序位于后台或被杀死时触发通知,如果你们能启发我,我会很感激的 这是我的服务课 从Android Oreo开始,如果前台服务不可见,所有后台服务都将被删除。解决问题的最佳方案是使用作业调度器() 对于及时的基于事件的任务,Job Sceduler是唯一的选项,尽管它在支持库中不再可用

我正在制作一个todolist应用程序,其中用户需要添加一个任务以及日期和时间,我稍后使用它来触发通知,当应用程序位于前台时,它工作正常,但由于android oreo之后服务会受到限制,现在我不知道如何在应用程序位于后台或被杀死时触发通知,如果你们能启发我,我会很感激的

  • 这是我的服务课

从Android Oreo开始,如果前台服务不可见,所有后台服务都将被删除。解决问题的最佳方案是使用作业调度器()

对于及时的基于事件的任务,Job Sceduler是唯一的选项,尽管它在支持库中不再可用


对于Oreo及以上版本的设备,您还需要一个通知通道。

谢谢您的回答,但是是否可以使用作业调度程序在精确的时间(例如12:00)推送通知?是否可以在12:00推送通知
  class NotificationService(var context: FragmentActivity) : Service(){
    private lateinit var remindersViewModel: remindersViewModel
    private lateinit var compat : NotificationManagerCompat
    override fun onCreate() {
        super.onCreate()
        compat = NotificationManagerCompat.from(this)
        remindersViewModel = ViewModelProvider(context)[remindersViewModel::class.java]
    }
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        isNotificationEnabled()

        return START_NOT_STICKY
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    private fun isNotificationEnabled(){
        //TODO : NOTIFICATION
        val notificationPrefs = getSharedPreferences("notificationPrefs", Context.MODE_PRIVATE)
        val isNotificationEnabled = notificationPrefs.getBoolean("notification", false)

         remindersViewModel.getAllTasks().observe(context, Observer {
                 if(isNotificationEnabled){
                     CoroutineScope(Dispatchers.Main).launch {
                         delay(3000)
                         HandleOperations.taskNotification(it, context, compat)
                     }
             }
         })


    }
}