Try calling startForeground in onCreate.
This commit is contained in:
parent
4d2ce7a2be
commit
29796f51d7
2 changed files with 28 additions and 10 deletions
|
@ -35,7 +35,25 @@ class FcmFetchForegroundService : Service() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onCreate() {
|
||||||
|
Log.d(TAG, "onCreate()")
|
||||||
|
postForegroundNotification()
|
||||||
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
|
Log.d(TAG, "onStartCommand()")
|
||||||
|
postForegroundNotification()
|
||||||
|
|
||||||
|
return if (intent != null && intent.getBooleanExtra(KEY_STOP_SELF, false)) {
|
||||||
|
stopForeground(true)
|
||||||
|
stopSelf()
|
||||||
|
START_NOT_STICKY
|
||||||
|
} else {
|
||||||
|
START_STICKY
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun postForegroundNotification() {
|
||||||
startForeground(
|
startForeground(
|
||||||
NotificationIds.FCM_FETCH,
|
NotificationIds.FCM_FETCH,
|
||||||
NotificationCompat.Builder(this, NotificationChannels.getInstance().OTHER)
|
NotificationCompat.Builder(this, NotificationChannels.getInstance().OTHER)
|
||||||
|
@ -47,14 +65,6 @@ class FcmFetchForegroundService : Service() {
|
||||||
.setVibrate(longArrayOf(0))
|
.setVibrate(longArrayOf(0))
|
||||||
.build()
|
.build()
|
||||||
)
|
)
|
||||||
|
|
||||||
return if (intent != null && intent.getBooleanExtra(KEY_STOP_SELF, false)) {
|
|
||||||
stopForeground(true)
|
|
||||||
stopSelf()
|
|
||||||
START_NOT_STICKY
|
|
||||||
} else {
|
|
||||||
START_STICKY
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
|
|
|
@ -459,9 +459,19 @@ class IncomingMessageObserver(private val context: Application) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onCreate() {
|
||||||
|
postForegroundNotification()
|
||||||
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
super.onStartCommand(intent, flags, startId)
|
super.onStartCommand(intent, flags, startId)
|
||||||
|
|
||||||
|
postForegroundNotification()
|
||||||
|
|
||||||
|
return START_STICKY
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun postForegroundNotification() {
|
||||||
val notification = NotificationCompat.Builder(applicationContext, NotificationChannels.getInstance().BACKGROUND)
|
val notification = NotificationCompat.Builder(applicationContext, NotificationChannels.getInstance().BACKGROUND)
|
||||||
.setContentTitle(applicationContext.getString(R.string.MessageRetrievalService_signal))
|
.setContentTitle(applicationContext.getString(R.string.MessageRetrievalService_signal))
|
||||||
.setContentText(applicationContext.getString(R.string.MessageRetrievalService_background_connection_enabled))
|
.setContentText(applicationContext.getString(R.string.MessageRetrievalService_background_connection_enabled))
|
||||||
|
@ -471,8 +481,6 @@ class IncomingMessageObserver(private val context: Application) {
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
startForeground(FOREGROUND_ID, notification)
|
startForeground(FOREGROUND_ID, notification)
|
||||||
|
|
||||||
return START_STICKY
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue