Skip to content

Commit de9ea13

Browse files
committed
add request to send broadcast data
1 parent bf69228 commit de9ea13

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

controller/notification_controller.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func notificationController() {
1111

1212
router.POST("/notification/test/:device", sendTestNotification)
1313
router.POST("/notification/:device", sendNotification)
14+
router.POST("/notification/broadcast", sendBroadcast)
1415

1516
router.OPTIONS("/notification/test/:device", okay)
1617
router.OPTIONS("/notification/:device", okay)
@@ -58,3 +59,28 @@ func sendNotification(c *gin.Context) {
5859
Body: body,
5960
})
6061
}
62+
63+
func sendBroadcast(c *gin.Context) {
64+
65+
if failIfNotRoot(c) {
66+
return
67+
}
68+
69+
var request dto.BroadcastRequestDto
70+
if err := c.BindJSON(&request); err != nil {
71+
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
72+
return
73+
}
74+
75+
apnsRequestId, apnsUniqueId, body, status, err := service.SendPushBroadcast(request.Channel, request.Content)
76+
if err != nil {
77+
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
78+
return
79+
}
80+
81+
c.IndentedJSON(status, dto.BroadcastResponseDto{
82+
ApnsRequestId: apnsRequestId,
83+
ApnsUniqueId: apnsUniqueId,
84+
Body: body,
85+
})
86+
}

dto/broadcast_request.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dto
2+
3+
type BroadcastRequestDto struct {
4+
Channel string `json:"channel"`
5+
Content string `json:"content"`
6+
}
7+
8+
type BroadcastResponseDto struct {
9+
ApnsRequestId string `json:"apns_request_id"`
10+
ApnsUniqueId string `json:"apns_unique_id"`
11+
Body string `json:"body"`
12+
}

service/broadcast_service.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package service
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"github.com/swimresults/user-service/apns"
7+
"golang.org/x/net/http2"
8+
"io/ioutil"
9+
"net/http"
10+
"strconv"
11+
"time"
12+
)
13+
14+
func SendPushBroadcast(channel string, content string) (string, string, string, int, error) {
15+
token := apns.GetToken()
16+
17+
t := &http2.Transport{}
18+
c := &http.Client{
19+
Transport: t,
20+
}
21+
22+
b := []byte(`
23+
{
24+
"aps": {
25+
"timestamp": ` + strconv.Itoa(int(time.Now().Unix())) + `,
26+
"event": "update",
27+
"content-state": {
28+
` + content + `
29+
}
30+
}
31+
}
32+
`)
33+
34+
r, err := http.NewRequest("POST", apnsUrl+"/4/broadcasts/apps/de.logilutions.SwimResults", bytes.NewBuffer(b))
35+
if err != nil {
36+
fmt.Println(err)
37+
}
38+
r.Header.Set("Content-Type", "application/json")
39+
r.Header.Set("Authorization", "Bearer "+token)
40+
41+
r.Header.Set("apns-push-type", "liveactivity")
42+
r.Header.Set("apns-expiration", "0")
43+
r.Header.Set("apns-priority", "10")
44+
r.Header.Set("apns-channel-id", channel)
45+
46+
println("making broadcast request with token '" + token + "'")
47+
48+
resp, err := c.Do(r)
49+
if err != nil {
50+
fmt.Println(err)
51+
}
52+
defer resp.Body.Close()
53+
54+
body, err := ioutil.ReadAll(resp.Body)
55+
if err != nil {
56+
fmt.Println(err)
57+
}
58+
fmt.Println(string(body))
59+
60+
apnsRequestID := resp.Header.Get("apns-request-id")
61+
apnsUID := resp.Header.Get("apns-unique-id")
62+
println("apns-request-id: " + apnsRequestID)
63+
println("apns-unique-id: " + apnsUID)
64+
65+
return apnsRequestID, apnsUID, string(body), resp.StatusCode, nil
66+
}

0 commit comments

Comments
 (0)