Skip to content

Commit 2164595

Browse files
committed
feat/qg-290: добавлены топики веб-пушей
1 parent f5d31f4 commit 2164595

3 files changed

Lines changed: 24 additions & 13 deletions

File tree

app/src/main/kotlin/pro/qyoga/app/therapist/appointments/notifications/SendFillScheduleNotificationsOp.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import java.time.ZoneOffset
1414

1515

1616
val fillScheduleWebPush = WebPush(
17-
"Не забудьте заполнить расписание",
18-
"Открыть приложение",
19-
URI.create(GetCalendarAppointmentsOp.PATH)
17+
title = "Не забудьте заполнить расписание",
18+
body = "Открыть приложение",
19+
deepLink = URI.create(GetCalendarAppointmentsOp.PATH),
20+
topic = "fill-schedule"
2021
)
2122

2223
@Component

app/src/main/kotlin/pro/qyoga/i9ns/pushes/web/WebPushServiceClient.kt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package pro.qyoga.i9ns.pushes.web
22

3-
import nl.martijndwars.webpush.Encoding
4-
import nl.martijndwars.webpush.Notification
5-
import nl.martijndwars.webpush.PushService
3+
import nl.martijndwars.webpush.*
4+
import org.bouncycastle.jce.interfaces.ECPublicKey
65
import org.bouncycastle.jce.provider.BouncyCastleProvider
76
import org.slf4j.LoggerFactory
87
import org.springframework.beans.factory.annotation.Value
98
import org.springframework.stereotype.Component
109
import pro.qyoga.i9ns.pushes.web.model.WebPush
1110
import pro.qyoga.i9ns.pushes.web.model.WebPushSubscription
1211
import java.security.Security
12+
import java.time.Duration
13+
import java.util.*
1314

1415

1516
@Component
@@ -31,12 +32,7 @@ class WebPushServiceClient(
3132
)
3233

3334
fun sendPush(subscription: WebPushSubscription, push: WebPush) {
34-
val notification = Notification(
35-
subscription.endpoint,
36-
subscription.keys.p256dh,
37-
subscription.keys.auth,
38-
push.toPayloadJson()
39-
)
35+
val notification = createNotification(subscription, push)
4036

4137
val resp = pushService.send(notification, Encoding.AES128GCM)
4238
if (resp.statusLine.statusCode != 201) {
@@ -50,6 +46,19 @@ class WebPushServiceClient(
5046

5147
}
5248

49+
private fun createNotification(
50+
subscription: WebPushSubscription,
51+
push: WebPush
52+
): Notification = Notification(
53+
/* endpoint = */ subscription.endpoint,
54+
/* userPublicKey = */ Utils.loadPublicKey(subscription.keys.p256dh) as ECPublicKey,
55+
/* userAuth = */ Base64.getUrlDecoder().decode(subscription.keys.auth),
56+
/* payload = */ push.toPayloadJson().toByteArray(),
57+
/* ttl = */ Duration.ofDays(7).toSeconds().toInt(),
58+
/* urgency = */ Urgency.NORMAL,
59+
/* topic = */ push.topic
60+
)
61+
5362
private fun WebPush.toPayloadJson() =
5463
"""
5564
{

app/src/main/kotlin/pro/qyoga/i9ns/pushes/web/model/WebPush.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ import java.net.URI
66
data class WebPush(
77
val title: String,
88
val body: String,
9-
val deepLink: URI
9+
val deepLink: URI,
10+
val topic: String
1011
)

0 commit comments

Comments
 (0)