2021-11-12 01:41:29 +01:00
|
|
|
package io.heckel.ntfy.msg
|
|
|
|
|
|
|
|
import android.app.NotificationChannel
|
|
|
|
import android.app.NotificationManager
|
|
|
|
import android.app.PendingIntent
|
|
|
|
import android.app.TaskStackBuilder
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.media.RingtoneManager
|
|
|
|
import android.os.Build
|
|
|
|
import android.util.Log
|
2021-11-27 22:18:09 +01:00
|
|
|
import androidx.annotation.RequiresApi
|
2021-11-12 01:41:29 +01:00
|
|
|
import androidx.core.app.NotificationCompat
|
2021-11-23 16:52:27 +01:00
|
|
|
import androidx.core.content.ContextCompat
|
2021-11-12 01:41:29 +01:00
|
|
|
import io.heckel.ntfy.R
|
2021-11-15 22:24:31 +01:00
|
|
|
import io.heckel.ntfy.data.Notification
|
2021-11-14 01:26:37 +01:00
|
|
|
import io.heckel.ntfy.data.Subscription
|
2021-11-12 01:41:29 +01:00
|
|
|
import io.heckel.ntfy.ui.DetailActivity
|
|
|
|
import io.heckel.ntfy.ui.MainActivity
|
2021-11-27 22:18:09 +01:00
|
|
|
import io.heckel.ntfy.util.formatMessage
|
|
|
|
import io.heckel.ntfy.util.formatTitle
|
2021-11-12 01:41:29 +01:00
|
|
|
|
|
|
|
class NotificationService(val context: Context) {
|
2021-11-15 22:24:31 +01:00
|
|
|
fun send(subscription: Subscription, notification: Notification) {
|
2021-11-27 22:18:09 +01:00
|
|
|
Log.d(TAG, "Displaying notification $notification")
|
2021-11-12 01:41:29 +01:00
|
|
|
|
|
|
|
// Create an Intent for the activity you want to start
|
|
|
|
val intent = Intent(context, DetailActivity::class.java)
|
|
|
|
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_ID, subscription.id)
|
|
|
|
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_BASE_URL, subscription.baseUrl)
|
|
|
|
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_TOPIC, subscription.topic)
|
2021-11-15 22:24:31 +01:00
|
|
|
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_INSTANT, subscription.instant)
|
2021-11-22 21:45:43 +01:00
|
|
|
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_MUTED_UNTIL, subscription.mutedUntil)
|
2021-11-12 01:41:29 +01:00
|
|
|
val pendingIntent: PendingIntent? = TaskStackBuilder.create(context).run {
|
|
|
|
addNextIntentWithParentStack(intent) // Add the intent, which inflates the back stack
|
|
|
|
getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT) // Get the PendingIntent containing the entire back stack
|
|
|
|
}
|
|
|
|
|
2021-11-27 22:18:09 +01:00
|
|
|
val title = formatTitle(subscription, notification)
|
|
|
|
val message = formatMessage(notification)
|
2021-11-12 01:41:29 +01:00
|
|
|
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
|
2021-11-27 22:18:09 +01:00
|
|
|
val channelId = toChannelId(notification.priority)
|
2021-11-29 20:06:08 +01:00
|
|
|
val notificationBuilder = NotificationCompat.Builder(context, channelId)
|
2021-11-23 16:52:27 +01:00
|
|
|
.setSmallIcon(R.drawable.ic_notification)
|
|
|
|
.setColor(ContextCompat.getColor(context, R.color.primaryColor))
|
2021-11-12 01:41:29 +01:00
|
|
|
.setContentTitle(title)
|
2021-11-27 22:18:09 +01:00
|
|
|
.setContentText(message)
|
|
|
|
.setStyle(NotificationCompat.BigTextStyle().bigText(message))
|
2021-11-12 01:41:29 +01:00
|
|
|
.setSound(defaultSoundUri)
|
|
|
|
.setContentIntent(pendingIntent) // Click target for notification
|
|
|
|
.setAutoCancel(true) // Cancel when notification is clicked
|
|
|
|
|
|
|
|
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
2021-11-29 20:06:08 +01:00
|
|
|
maybeCreateNotificationChannel(notificationManager, notification.priority)
|
2021-11-15 22:24:31 +01:00
|
|
|
notificationManager.notify(notification.notificationId, notificationBuilder.build())
|
|
|
|
}
|
|
|
|
|
|
|
|
fun cancel(notification: Notification) {
|
|
|
|
if (notification.notificationId != 0) {
|
|
|
|
Log.d(TAG, "Cancelling notification ${notification.id}: ${notification.message}")
|
|
|
|
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
notificationManager.cancel(notification.notificationId)
|
|
|
|
}
|
2021-11-12 01:41:29 +01:00
|
|
|
}
|
|
|
|
|
2021-11-29 20:06:08 +01:00
|
|
|
fun createNotificationChannels() {
|
|
|
|
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
(1..5).forEach { priority -> maybeCreateNotificationChannel(notificationManager, priority) }
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun maybeCreateNotificationChannel(notificationManager: NotificationManager, priority: Int) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
// Note: To change a notification channel, you must delete the old one and create a new one!
|
|
|
|
|
|
|
|
val pause = 300L
|
|
|
|
val channel = when (priority) {
|
|
|
|
1 -> NotificationChannel(CHANNEL_ID_MIN, context.getString(R.string.channel_notifications_min_name), NotificationManager.IMPORTANCE_MIN)
|
|
|
|
2 -> NotificationChannel(CHANNEL_ID_LOW, context.getString(R.string.channel_notifications_low_name), NotificationManager.IMPORTANCE_LOW)
|
|
|
|
4 -> {
|
|
|
|
val channel = NotificationChannel(CHANNEL_ID_HIGH, context.getString(R.string.channel_notifications_high_name), NotificationManager.IMPORTANCE_HIGH)
|
|
|
|
channel.enableVibration(true)
|
|
|
|
channel.vibrationPattern = longArrayOf(
|
|
|
|
pause, 100, pause, 100, pause, 100,
|
|
|
|
pause, 2000
|
|
|
|
)
|
|
|
|
channel
|
|
|
|
}
|
|
|
|
5 -> {
|
|
|
|
val channel = NotificationChannel(CHANNEL_ID_MAX, context.getString(R.string.channel_notifications_max_name), NotificationManager.IMPORTANCE_MAX)
|
|
|
|
channel.enableLights(true)
|
|
|
|
channel.enableVibration(true)
|
|
|
|
channel.vibrationPattern = longArrayOf(
|
|
|
|
pause, 100, pause, 100, pause, 100,
|
|
|
|
pause, 2000,
|
|
|
|
pause, 100, pause, 100, pause, 100,
|
|
|
|
pause, 2000,
|
|
|
|
pause, 100, pause, 100, pause, 100,
|
|
|
|
pause, 2000
|
|
|
|
)
|
|
|
|
channel
|
|
|
|
}
|
|
|
|
else -> NotificationChannel(CHANNEL_ID_DEFAULT, context.getString(R.string.channel_notifications_default_name), NotificationManager.IMPORTANCE_DEFAULT)
|
|
|
|
}
|
|
|
|
notificationManager.createNotificationChannel(channel)
|
2021-11-27 22:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun toChannelId(priority: Int): String {
|
|
|
|
return when (priority) {
|
|
|
|
1 -> CHANNEL_ID_MIN
|
|
|
|
2 -> CHANNEL_ID_LOW
|
|
|
|
4 -> CHANNEL_ID_HIGH
|
|
|
|
5 -> CHANNEL_ID_MAX
|
|
|
|
else -> CHANNEL_ID_DEFAULT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 01:41:29 +01:00
|
|
|
companion object {
|
|
|
|
private const val TAG = "NtfyNotificationService"
|
2021-11-27 22:18:09 +01:00
|
|
|
private const val CHANNEL_ID_MIN = "ntfy-min"
|
|
|
|
private const val CHANNEL_ID_LOW = "ntfy-low"
|
|
|
|
private const val CHANNEL_ID_DEFAULT = "ntfy"
|
|
|
|
private const val CHANNEL_ID_HIGH = "ntfy-high"
|
|
|
|
private const val CHANNEL_ID_MAX = "ntfy-max"
|
2021-11-12 01:41:29 +01:00
|
|
|
}
|
|
|
|
}
|