mirror of
https://github.com/afollestad/nock-nock.git
synced 2025-08-03 22:58:38 +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))
|
list.addItemDecoration(DividerItemDecoration(this, VERTICAL))
|
||||||
|
|
||||||
fab.setOnClickListener(this)
|
fab.setOnClickListener(this)
|
||||||
|
notificationManager.createChannels()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
|
|
|
@ -5,12 +5,15 @@
|
||||||
*/
|
*/
|
||||||
package com.afollestad.nocknock.notifications
|
package com.afollestad.nocknock.notifications
|
||||||
|
|
||||||
|
import android.annotation.TargetApi
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
import android.app.PendingIntent
|
import android.app.PendingIntent
|
||||||
import android.app.PendingIntent.FLAG_CANCEL_CURRENT
|
import android.app.PendingIntent.FLAG_CANCEL_CURRENT
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
|
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 androidx.core.app.NotificationCompat.DEFAULT_VIBRATE
|
||||||
import com.afollestad.nocknock.data.ServerModel
|
import com.afollestad.nocknock.data.ServerModel
|
||||||
import com.afollestad.nocknock.notifications.Channel.Statuses
|
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 com.afollestad.nocknock.utilities.util.hasOreo
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
const val STATUS_NOTIFICATION_ID = 3456
|
|
||||||
|
|
||||||
/** @author Aidan Follestad (@afollestad) */
|
/** @author Aidan Follestad (@afollestad) */
|
||||||
interface NockNotificationManager {
|
interface NockNotificationManager {
|
||||||
|
|
||||||
|
@ -48,12 +49,19 @@ class RealNockNotificationManager @Inject constructor(
|
||||||
private const val BASE_REQUEST_CODE = 44
|
private const val BASE_REQUEST_CODE = 44
|
||||||
|
|
||||||
const val KEY_MODEL = "model"
|
const val KEY_MODEL = "model"
|
||||||
|
|
||||||
|
private fun log(message: String) {
|
||||||
|
if (BuildConfig.DEBUG) {
|
||||||
|
Log.d("NockNotificationManager", message)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var isAppOpen = false
|
private var isAppOpen = false
|
||||||
|
|
||||||
override fun setIsAppOpen(open: Boolean) {
|
override fun setIsAppOpen(open: Boolean) {
|
||||||
this.isAppOpen = open
|
this.isAppOpen = open
|
||||||
|
log("Is app open? $open")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun createChannels() {
|
override fun createChannels() {
|
||||||
|
@ -64,9 +72,11 @@ class RealNockNotificationManager @Inject constructor(
|
||||||
override fun postStatusNotification(model: ServerModel) {
|
override fun postStatusNotification(model: ServerModel) {
|
||||||
if (isAppOpen) {
|
if (isAppOpen) {
|
||||||
// Don't show notifications while the app is open
|
// Don't show notifications while the app is open
|
||||||
|
log("App is open, status notification for site ${model.id} won't be posted.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log("Posting status notification for site ${model.id}...")
|
||||||
val viewSiteActivityCls =
|
val viewSiteActivityCls =
|
||||||
Class.forName("com.afollestad.nocknock.ui.ViewSiteActivity")
|
Class.forName("com.afollestad.nocknock.ui.ViewSiteActivity")
|
||||||
val openIntent = Intent(app, viewSiteActivityCls).apply {
|
val openIntent = Intent(app, viewSiteActivityCls).apply {
|
||||||
|
@ -90,22 +100,29 @@ class RealNockNotificationManager @Inject constructor(
|
||||||
setDefaults(DEFAULT_VIBRATE)
|
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) {
|
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() {
|
override fun cancelStatusNotifications() {
|
||||||
stockManager.cancelAll()
|
stockManager.cancelAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TargetApi(VERSION_CODES.O)
|
||||||
private fun createChannel(channel: Channel) {
|
private fun createChannel(channel: Channel) {
|
||||||
if (!hasOreo()) {
|
if (!hasOreo()) {
|
||||||
|
log("Not running Android O, channels won't be created.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val notificationChannel = channel.toNotificationChannel(app)
|
val notificationChannel = channel.toNotificationChannel(app)
|
||||||
stockManager.createNotificationChannel(notificationChannel)
|
stockManager.createNotificationChannel(notificationChannel)
|
||||||
|
log("Created notification channel ${channel.id}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun ServerModel.notificationId() = BASE_REQUEST_CODE + this.id
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue