Skip to content

Commit

Permalink
add notification request
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad2002 committed Nov 21, 2024
1 parent 891c555 commit 175ff5f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
33 changes: 30 additions & 3 deletions controller/notification_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package controller

import (
"github.com/gin-gonic/gin"
"github.com/swimresults/user-service/dto"
"github.com/swimresults/user-service/service"
"net/http"
)

func notificationController() {

router.POST("/notification/test/:device", sendTestNotification)
router.POST("/notification/test/:device", sendTestNotification)
router.POST("/notification/:device", sendNotification)

router.OPTIONS("/notification/test", okay)
router.OPTIONS("/notification/test", okay)
router.OPTIONS("/notification/test/:device", okay)
router.OPTIONS("/notification/:device", okay)
}

func sendTestNotification(c *gin.Context) {
Expand All @@ -31,3 +32,29 @@ func sendTestNotification(c *gin.Context) {

c.Status(http.StatusOK)
}

func sendNotification(c *gin.Context) {

if failIfNotRoot(c) {
return
}

device := c.Param("device")

var request dto.NotificationRequestDto
if err := c.BindJSON(&request); err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}

apnsId, body, status, err := service.SendPushNotification(device, request.Title, request.Subtitle, request.Message)
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}

c.IndentedJSON(status, dto.NotificationResponseDto{
ApnsId: apnsId,
Body: body,
})
}
12 changes: 12 additions & 0 deletions dto/notification_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dto

type NotificationRequestDto struct {
Title string `json:"title"`
Subtitle string `json:"subtitle"`
Message string `json:"message"`
}

type NotificationResponseDto struct {
ApnsId string `json:"apns_id"`
Body string `json:"body"`
}
55 changes: 55 additions & 0 deletions service/notification_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"golang.org/x/net/http2"
"io/ioutil"
"net/http"
"os"
)

var apnsUrl = os.Getenv("SR_APNS_URL")

func SendTestPushNotification(receiver string) error {
token := apns.GetToken()

Expand Down Expand Up @@ -57,3 +60,55 @@ func SendTestPushNotification(receiver string) error {

return nil
}

func SendPushNotification(receiver string, title string, subtitle string, message string) (string, string, int, error) {
token := apns.GetToken()

t := &http2.Transport{}
c := &http.Client{
Transport: t,
}

b := []byte(`
{
"aps": {
"alert": {
"title": "` + title + `",
"subtitle": "` + subtitle + `",
"body": "` + message + `"
}
}
}
`)

r, err := http.NewRequest("POST", apnsUrl+"/3/device/"+receiver, bytes.NewBuffer(b))
if err != nil {
fmt.Println(err)
}
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Authorization", "Bearer "+token)

r.Header.Set("apns-push-type", "alert")
r.Header.Set("apns-expiration", "0")
r.Header.Set("apns-priority", "10")
r.Header.Set("apns-topic", "de.logilutions.SwimResults")

println("making request with token '" + token + "'")

resp, err := c.Do(r)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(body))

apnsID := resp.Header.Get("apns-id")
println("apns-id: " + apnsID)

return apnsID, string(body), resp.StatusCode, nil
}

0 comments on commit 175ff5f

Please sign in to comment.