forked from joreilly/Confetti
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionNotificationBuilder.kt
More file actions
77 lines (70 loc) · 3.21 KB
/
SessionNotificationBuilder.kt
File metadata and controls
77 lines (70 loc) · 3.21 KB
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
package dev.johnoreilly.confetti.notifications
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat
import androidx.core.net.toUri
import dev.johnoreilly.confetti.fragment.SessionDetails
import dev.johnoreilly.confetti.shared.R
import dev.johnoreilly.confetti.work.SessionNotificationSender.Companion.CHANNEL_ID
import dev.johnoreilly.confetti.work.SessionNotificationSender.Companion.GROUP
class SessionNotificationBuilder(
private val context: Context,
) {
fun createNotification(session: SessionDetails, conferenceId: String, notificationId: Int): NotificationCompat.Builder {
val largeIcon = BitmapFactory.decodeResource(
context.resources,
R.mipmap.ic_launcher_round
)
return NotificationCompat
.Builder(context, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setLargeIcon(largeIcon)
.setContentTitle(session.title)
.setContentText("Starts at ${session.startsAt.time} in ${session.room?.name.orEmpty()}")
.setGroup(GROUP)
.setAutoCancel(false)
.setLocalOnly(false)
.setContentIntent(openSessionIntent(session, conferenceId, notificationId))
.addAction(unbookmarkAction(conferenceId, session.id, notificationId))
.extend(
NotificationCompat.WearableExtender()
.setBridgeTag("session:reminder")
)
}
private fun openSessionIntent(session: SessionDetails, conferenceId: String, notificationId: Int): PendingIntent? {
return PendingIntent.getActivity(
context,
notificationId,
Intent(Intent.ACTION_VIEW, "https://confetti-app.dev/conference/${conferenceId}/session/${session.id}".toUri()),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}
private fun unbookmarkAction(conferenceId: String, sessionId: String, notificationId: Int): NotificationCompat.Action {
val unbookmarkIntent = PendingIntent.getBroadcast(
context,
notificationId,
Intent(context, NotificationReceiver::class.java).apply {
action = "REMOVE_BOOKMARK"
putExtra("conferenceId", conferenceId)
putExtra("sessionId", sessionId)
putExtra("notificationId", notificationId)
},
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
return NotificationCompat.Action.Builder(null, "Remove Bookmark", unbookmarkIntent)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_ARCHIVE)
.build()
}
fun createChannel(): NotificationChannelCompat.Builder {
val name = "Upcoming sessions"
val importance = NotificationManager.IMPORTANCE_DEFAULT
return NotificationChannelCompat.Builder(CHANNEL_ID, importance)
.setName(name)
.setDescription("Session reminders for upcoming sessions")
.setShowBadge(true)
}
}