Vibration pattern

This commit is contained in:
Philipp Heckel 2021-11-29 14:06:08 -05:00
parent 4444c9de9c
commit 4ed73f6f98
6 changed files with 61 additions and 22776 deletions

View file

@ -13,7 +13,7 @@ android {
targetSdkVersion 30
versionCode 9
versionName "1.2.2"
versionName "1.3.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

View file

@ -6,7 +6,6 @@ import android.app.PendingIntent
import android.app.TaskStackBuilder
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.media.RingtoneManager
import android.os.Build
import android.util.Log
@ -16,7 +15,6 @@ import androidx.core.content.ContextCompat
import io.heckel.ntfy.R
import io.heckel.ntfy.data.Notification
import io.heckel.ntfy.data.Subscription
import io.heckel.ntfy.util.topicShortUrl
import io.heckel.ntfy.ui.DetailActivity
import io.heckel.ntfy.ui.MainActivity
import io.heckel.ntfy.util.formatMessage
@ -42,7 +40,7 @@ class NotificationService(val context: Context) {
val message = formatMessage(notification)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val channelId = toChannelId(notification.priority)
var notificationBuilder = NotificationCompat.Builder(context, channelId)
val notificationBuilder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setColor(ContextCompat.getColor(context, R.color.primaryColor))
.setContentTitle(title)
@ -52,20 +50,8 @@ class NotificationService(val context: Context) {
.setContentIntent(pendingIntent) // Click target for notification
.setAutoCancel(true) // Cancel when notification is clicked
if (notification.priority == 4) {
notificationBuilder = notificationBuilder
.setVibrate(longArrayOf(500, 500, 500, 500, 500, 500))
.setLights(Color.YELLOW, 3000, 3000)
} else if (notification.priority == 5) {
notificationBuilder = notificationBuilder
.setVibrate(longArrayOf(1000, 500, 1000, 500, 1000, 500))
.setLights(Color.RED, 3000, 3000)
}
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel(notificationManager, notification)
}
maybeCreateNotificationChannel(notificationManager, notification.priority)
notificationManager.notify(notification.notificationId, notificationBuilder.build())
}
@ -77,16 +63,46 @@ class NotificationService(val context: Context) {
}
}
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(notificationManager: NotificationManager, notification: Notification) {
val channel = when (notification.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 -> NotificationChannel(CHANNEL_ID_HIGH, context.getString(R.string.channel_notifications_high_name), NotificationManager.IMPORTANCE_HIGH)
5 -> NotificationChannel(CHANNEL_ID_MAX, context.getString(R.string.channel_notifications_max_name), NotificationManager.IMPORTANCE_MAX)
else -> NotificationChannel(CHANNEL_ID_DEFAULT, context.getString(R.string.channel_notifications_default_name), NotificationManager.IMPORTANCE_DEFAULT)
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)
}
notificationManager.createNotificationChannel(channel)
}
private fun toChannelId(priority: Int): String {

View file

@ -112,6 +112,9 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc
subscriberManager?.refreshService(it)
}
// Create notification channels right away, so we can configure them immediately after installing the app
notifier!!.createNotificationChannels()
// Background things
startPeriodicWorker()
}

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,12 @@
New features:
* Support for five priorities (via X-Priority, #24) mapped to Android priorities
* Support for notification title (via X-Title, #24)
* Support for tags and emojis (via X-Tags, #24)
Usability improvements:
* Support deep links for https://ntfy.sh/.. topics (#20)
* Add "clear all notifications" menu item (#21)
* Auto-complete for "use another server" feature (#13)
* Expanding notifications to read entire text (no ticket)
* Color "Permanently delete" buttons red (no ticket)

View file

@ -12,6 +12,6 @@ Example:
$ curl -d "Your backup is done" ntfy.sh/mytopic
Find more examples and usage instructions here:
Website: https://ntfy.sh
GitHub (server): https://github.com/binwiederhier/ntfy
GitHub (Android app): https://github.com/binwiederhier/ntfy-android
* Website: https://ntfy.sh
* GitHub (server): https://github.com/binwiederhier/ntfy
* GitHub (Android app): https://github.com/binwiederhier/ntfy-android