diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-06-08 16:44:44 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-06-17 14:15:14 +0000 |
| commit | a10e5d94e331a1929089d3d29408a653dbc4de58 (patch) | |
| tree | 8b7b9514644920d5cde65b75ecd3045d2812a8fd /android/src/main | |
| parent | de6ae31c36a7367a04ea424ca5f8e1b8470a38cf (diff) | |
| download | mullvadvpn-a10e5d94e331a1929089d3d29408a653dbc4de58.tar.xz mullvadvpn-a10e5d94e331a1929089d3d29408a653dbc4de58.zip | |
Create `NotificationChannel` helper class
Diffstat (limited to 'android/src/main')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationChannel.kt | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationChannel.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationChannel.kt new file mode 100644 index 0000000000..2546c8955f --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationChannel.kt @@ -0,0 +1,61 @@ +package net.mullvad.mullvadvpn.service.notifications + +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.os.Build +import android.support.v4.app.NotificationCompat +import net.mullvad.mullvadvpn.R + +class NotificationChannel( + val context: Context, + val id: String, + val name: Int, + val description: Int, + val importance: Int +) { + private val badgeColor by lazy { + context.resources.getColor(R.color.colorPrimary) + } + + val notificationManager = + context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + + init { + if (Build.VERSION.SDK_INT >= 26) { + val channelName = context.getString(name) + val channelDescription = context.getString(description) + + val channel = NotificationChannel(id, channelName, importance).apply { + description = channelDescription + setShowBadge(true) + } + + notificationManager.createNotificationChannel(channel) + } + } + + fun buildNotification(intent: PendingIntent, title: Int): Notification { + return buildNotification(intent, title, emptyList()) + } + + fun buildNotification( + pendingIntent: PendingIntent, + title: Int, + actions: List<NotificationCompat.Action> + ): Notification { + val builder = NotificationCompat.Builder(context, id) + .setSmallIcon(R.drawable.small_logo_black) + .setColor(badgeColor) + .setContentTitle(context.getString(title)) + .setContentIntent(pendingIntent) + + for (action in actions) { + builder.addAction(action) + } + + return builder.build() + } +} |
