-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathNotificationHelper.kt
92 lines (78 loc) · 2.87 KB
/
NotificationHelper.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.OxGames.Pluvia.service
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Context.NOTIFICATION_SERVICE
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.net.toUri
import com.OxGames.Pluvia.MainActivity
import com.OxGames.Pluvia.R
class NotificationHelper(private val context: Context) {
companion object {
private const val CHANNEL_ID = "pluvia_foreground_service"
private const val CHANNEL_NAME = "Pluvia Foreground Service"
private const val NOTIFICATION_ID = 1
const val ACTION_EXIT = "com.oxgames.pluvia.EXIT"
}
private val notificationManager: NotificationManager =
context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
init {
createNotificationChannel()
}
private fun createNotificationChannel() {
val channel = NotificationChannel(
CHANNEL_ID,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW,
).apply {
description = "Allows to display Pluvia foreground notifications"
setShowBadge(false)
}
notificationManager.createNotificationChannel(channel)
}
fun notify(content: String) {
val notification = createForegroundNotification(content)
notificationManager.notify(NOTIFICATION_ID, notification)
}
fun cancel() {
notificationManager.cancel(NOTIFICATION_ID)
}
fun createForegroundNotification(content: String): Notification {
val intent = Intent(
Intent.ACTION_VIEW,
"pluvia://home".toUri(),
context,
MainActivity::class.java,
).apply {
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
}
val pendingIntent = PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)
val stopIntent = Intent(context, SteamService::class.java).apply {
action = ACTION_EXIT
}
val stopPendingIntent = PendingIntent.getForegroundService(
context,
0,
stopIntent,
PendingIntent.FLAG_IMMUTABLE,
)
return NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(content)
.setSmallIcon(R.drawable.icon_mono_foreground)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setAutoCancel(false)
.setOngoing(true)
.setContentIntent(pendingIntent)
.addAction(0, "Exit", stopPendingIntent) // 0 = no icon
.build()
}
}