Merge pull request #959 from nightscout/AdrianLxM/notification-channels

make sure notification channel exists
This commit is contained in:
Milos Kozak 2021-11-21 10:50:26 +01:00 committed by GitHub
commit cd1bffe64a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 25 deletions

View file

@ -65,10 +65,10 @@ class PersistentNotificationPlugin @Inject constructor(
// End Android auto // End Android auto
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
private var channel: NotificationChannel? = null
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
notificationHolder.createNotificationChannel()
disposable += rxBus disposable += rxBus
.toObservable(EventRefreshOverview::class.java) .toObservable(EventRefreshOverview::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
@ -103,12 +103,6 @@ class PersistentNotificationPlugin @Inject constructor(
.subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException) .subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException)
} }
private fun createNotificationChannel() {
val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
channel = NotificationChannel(notificationHolder.channelID, notificationHolder.channelID as CharSequence, NotificationManager.IMPORTANCE_HIGH)
channel?.let { mNotificationManager.createNotificationChannel(it) }
}
override fun onStop() { override fun onStop() {
disposable.clear() disposable.clear()
dummyServiceHelper.stopService(context) dummyServiceHelper.stopService(context)
@ -116,8 +110,6 @@ class PersistentNotificationPlugin @Inject constructor(
} }
private fun triggerNotificationUpdate() { private fun triggerNotificationUpdate() {
if (channel == null)
createNotificationChannel() // make sure channels exist before triggering updates through the bus
updateNotification() updateNotification()
dummyServiceHelper.startService(context) dummyServiceHelper.startService(context)
} }

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.utils.androidNotification package info.nightscout.androidaps.utils.androidNotification
import android.app.Notification import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context import android.content.Context
@ -17,14 +18,31 @@ import javax.inject.Singleton
@Singleton @Singleton
class NotificationHolderImpl @Inject constructor( class NotificationHolderImpl @Inject constructor(
rh: ResourceHelper, val rh: ResourceHelper,
context: Context, val context: Context,
iconsProvider: IconsProvider val iconsProvider: IconsProvider
) : NotificationHolder { ) : NotificationHolder {
override val channelID = "AndroidAPS-Ongoing" override val channelID = "AndroidAPS-Ongoing"
override val notificationID = 4711 override val notificationID = 4711
override var notification: Notification = NotificationCompat.Builder(context, channelID) private var _notification: Notification? = null
override var notification: Notification
set(value) {
_notification = value
}
get() = _notification ?: placeholderNotification()
override fun openAppIntent(context: Context): PendingIntent? = TaskStackBuilder.create(context).run {
addParentStack(MainActivity::class.java)
addNextIntent(Intent(context, MainActivity::class.java))
getPendingIntent(0, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun placeholderNotification(): Notification {
createNotificationChannel()
return NotificationCompat.Builder(context, channelID)
.setOngoing(true) .setOngoing(true)
.setOnlyAlertOnce(true) .setOnlyAlertOnce(true)
.setCategory(NotificationCompat.CATEGORY_STATUS) .setCategory(NotificationCompat.CATEGORY_STATUS)
@ -36,10 +54,11 @@ class NotificationHolderImpl @Inject constructor(
.also { .also {
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(notificationID, it) (context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(notificationID, it)
} }
}
override fun openAppIntent(context: Context): PendingIntent? = TaskStackBuilder.create(context).run { override fun createNotificationChannel() {
addParentStack(MainActivity::class.java) val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
addNextIntent(Intent(context, MainActivity::class.java)) val channel = NotificationChannel(channelID, channelID as CharSequence, NotificationManager.IMPORTANCE_HIGH)
getPendingIntent(0, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) mNotificationManager.createNotificationChannel(channel)
} }
} }

View file

@ -10,4 +10,5 @@ interface NotificationHolder {
var notification: Notification var notification: Notification
fun openAppIntent(context: Context): PendingIntent? fun openAppIntent(context: Context): PendingIntent?
fun createNotificationChannel()
} }