mirror of
https://github.com/afollestad/nock-nock.git
synced 2025-04-20 03:25:14 +00:00
Create notification channels when app is startwd, add logging to notification manager
This commit is contained in:
parent
0785c36b2a
commit
d1672a6c5e
2 changed files with 22 additions and 4 deletions
|
@ -112,6 +112,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
|
|||
list.addItemDecoration(DividerItemDecoration(this, VERTICAL))
|
||||
|
||||
fab.setOnClickListener(this)
|
||||
notificationManager.createChannels()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
|
|
|
@ -5,12 +5,15 @@
|
|||
*/
|
||||
package com.afollestad.nocknock.notifications
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.app.Application
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.app.PendingIntent.FLAG_CANCEL_CURRENT
|
||||
import android.content.Intent
|
||||
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
import android.os.Build.VERSION_CODES
|
||||
import android.util.Log
|
||||
import androidx.core.app.NotificationCompat.DEFAULT_VIBRATE
|
||||
import com.afollestad.nocknock.data.ServerModel
|
||||
import com.afollestad.nocknock.notifications.Channel.Statuses
|
||||
|
@ -20,8 +23,6 @@ import com.afollestad.nocknock.utilities.qualifiers.AppIconRes
|
|||
import com.afollestad.nocknock.utilities.util.hasOreo
|
||||
import javax.inject.Inject
|
||||
|
||||
const val STATUS_NOTIFICATION_ID = 3456
|
||||
|
||||
/** @author Aidan Follestad (@afollestad) */
|
||||
interface NockNotificationManager {
|
||||
|
||||
|
@ -48,12 +49,19 @@ class RealNockNotificationManager @Inject constructor(
|
|||
private const val BASE_REQUEST_CODE = 44
|
||||
|
||||
const val KEY_MODEL = "model"
|
||||
|
||||
private fun log(message: String) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.d("NockNotificationManager", message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var isAppOpen = false
|
||||
|
||||
override fun setIsAppOpen(open: Boolean) {
|
||||
this.isAppOpen = open
|
||||
log("Is app open? $open")
|
||||
}
|
||||
|
||||
override fun createChannels() {
|
||||
|
@ -64,9 +72,11 @@ class RealNockNotificationManager @Inject constructor(
|
|||
override fun postStatusNotification(model: ServerModel) {
|
||||
if (isAppOpen) {
|
||||
// Don't show notifications while the app is open
|
||||
log("App is open, status notification for site ${model.id} won't be posted.")
|
||||
return
|
||||
}
|
||||
|
||||
log("Posting status notification for site ${model.id}...")
|
||||
val viewSiteActivityCls =
|
||||
Class.forName("com.afollestad.nocknock.ui.ViewSiteActivity")
|
||||
val openIntent = Intent(app, viewSiteActivityCls).apply {
|
||||
|
@ -90,22 +100,29 @@ class RealNockNotificationManager @Inject constructor(
|
|||
setDefaults(DEFAULT_VIBRATE)
|
||||
}
|
||||
|
||||
stockManager.notify(model.url, STATUS_NOTIFICATION_ID, newNotification)
|
||||
stockManager.notify(model.url, model.notificationId(), newNotification)
|
||||
log("Posted status notification for site ${model.notificationId()}.")
|
||||
}
|
||||
|
||||
override fun cancelStatusNotification(model: ServerModel) {
|
||||
stockManager.cancel(BASE_REQUEST_CODE + model.id)
|
||||
stockManager.cancel(model.notificationId())
|
||||
log("Cancelled status notification for site ${model.id}.")
|
||||
}
|
||||
|
||||
override fun cancelStatusNotifications() {
|
||||
stockManager.cancelAll()
|
||||
}
|
||||
|
||||
@TargetApi(VERSION_CODES.O)
|
||||
private fun createChannel(channel: Channel) {
|
||||
if (!hasOreo()) {
|
||||
log("Not running Android O, channels won't be created.")
|
||||
return
|
||||
}
|
||||
val notificationChannel = channel.toNotificationChannel(app)
|
||||
stockManager.createNotificationChannel(notificationChannel)
|
||||
log("Created notification channel ${channel.id}")
|
||||
}
|
||||
|
||||
private fun ServerModel.notificationId() = BASE_REQUEST_CODE + this.id
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue