Merge pull request #959 from nightscout/AdrianLxM/notification-channels
make sure notification channel exists
This commit is contained in:
commit
cd1bffe64a
3 changed files with 37 additions and 25 deletions
|
@ -65,10 +65,10 @@ class PersistentNotificationPlugin @Inject constructor(
|
|||
// End Android auto
|
||||
|
||||
private val disposable = CompositeDisposable()
|
||||
private var channel: NotificationChannel? = null
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
notificationHolder.createNotificationChannel()
|
||||
disposable += rxBus
|
||||
.toObservable(EventRefreshOverview::class.java)
|
||||
.observeOn(aapsSchedulers.io)
|
||||
|
@ -103,12 +103,6 @@ class PersistentNotificationPlugin @Inject constructor(
|
|||
.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() {
|
||||
disposable.clear()
|
||||
dummyServiceHelper.stopService(context)
|
||||
|
@ -116,8 +110,6 @@ class PersistentNotificationPlugin @Inject constructor(
|
|||
}
|
||||
|
||||
private fun triggerNotificationUpdate() {
|
||||
if (channel == null)
|
||||
createNotificationChannel() // make sure channels exist before triggering updates through the bus
|
||||
updateNotification()
|
||||
dummyServiceHelper.startService(context)
|
||||
}
|
||||
|
@ -231,4 +223,4 @@ class PersistentNotificationPlugin @Inject constructor(
|
|||
mNotificationManager.notify(notificationHolder.notificationID, notification)
|
||||
notificationHolder.notification = notification
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package info.nightscout.androidaps.utils.androidNotification
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
|
@ -17,29 +18,47 @@ import javax.inject.Singleton
|
|||
|
||||
@Singleton
|
||||
class NotificationHolderImpl @Inject constructor(
|
||||
rh: ResourceHelper,
|
||||
context: Context,
|
||||
iconsProvider: IconsProvider
|
||||
val rh: ResourceHelper,
|
||||
val context: Context,
|
||||
val iconsProvider: IconsProvider
|
||||
) : NotificationHolder {
|
||||
|
||||
override val channelID = "AndroidAPS-Ongoing"
|
||||
override val notificationID = 4711
|
||||
override var notification: Notification = NotificationCompat.Builder(context, channelID)
|
||||
.setOngoing(true)
|
||||
.setOnlyAlertOnce(true)
|
||||
.setCategory(NotificationCompat.CATEGORY_STATUS)
|
||||
.setSmallIcon(iconsProvider.getNotificationIcon())
|
||||
.setLargeIcon(rh.decodeResource(iconsProvider.getIcon()))
|
||||
.setContentTitle(rh.gs(R.string.loading))
|
||||
.setContentIntent(openAppIntent(context))
|
||||
.build()
|
||||
.also {
|
||||
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(notificationID, it)
|
||||
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)
|
||||
.setOnlyAlertOnce(true)
|
||||
.setCategory(NotificationCompat.CATEGORY_STATUS)
|
||||
.setSmallIcon(iconsProvider.getNotificationIcon())
|
||||
.setLargeIcon(rh.decodeResource(iconsProvider.getIcon()))
|
||||
.setContentTitle(rh.gs(R.string.loading))
|
||||
.setContentIntent(openAppIntent(context))
|
||||
.build()
|
||||
.also {
|
||||
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(notificationID, it)
|
||||
}
|
||||
}
|
||||
|
||||
override fun createNotificationChannel() {
|
||||
val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
val channel = NotificationChannel(channelID, channelID as CharSequence, NotificationManager.IMPORTANCE_HIGH)
|
||||
mNotificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,4 +10,5 @@ interface NotificationHolder {
|
|||
var notification: Notification
|
||||
|
||||
fun openAppIntent(context: Context): PendingIntent?
|
||||
}
|
||||
fun createNotificationChannel()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue