-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.go
More file actions
101 lines (78 loc) · 3.34 KB
/
send.go
File metadata and controls
101 lines (78 loc) · 3.34 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"encoding/json"
"fmt"
apns "github.com/anachronistic/apns"
"github.com/gin-gonic/gin"
"github.com/muto_gcm"
"github.com/parnurzeal/gorequest"
)
func main() {
r := gin.Default()
r.POST("/send", func(c *gin.Context) {
var combined muto_gcm.Combined
var notification muto_gcm.Notification
c.BindJSON(&combined)
// Mapping the Notification content and other options to the corresponding server
// i.e. GCM for Android and APNs for iOS
for _, val := range combined.DevicesList {
if val.Platform == "Android" {
notification.TargetDeviceIDs = notification.TargetDeviceIDs[:0] // Empty the slice for previous entries
notification.TargetDeviceIDs = append(notification.TargetDeviceIDs, val.DeviceID)
notification.CollapseKey = combined.CollapseKey
notification.Priority = combined.Priority
notification.ContentAvailable = combined.ContentAvailableGcm
notification.DelayWhileIdle = combined.DelayWhileIdle
notification.TimeToLive = combined.TimeToLive
notification.RestrictedPackageName = combined.RestrictedPackageName
notification.DryRun = combined.DryRun
notification.Payload.Title = combined.Contents.Title
notification.Payload.Body = combined.Contents.Body
notification.Payload.Icon = combined.Payload.Icon
notification.Payload.Sound = combined.Payload.Sound
notification.Payload.Tag = combined.Payload.Tag
notification.Payload.Color = combined.Payload.Color
notification.Payload.ClickAction = combined.Payload.ClickAction
notification.Payload.BodyLocKey = combined.Payload.BodyLocKey
notification.Payload.BodyLocArgs = combined.Payload.BodyLocArgs
notification.Payload.TitleLocKey = combined.Payload.TitleLocKey
notification.Payload.TitleLocArgs = combined.Payload.TitleLocArgs
b, _ := json.Marshal(notification) // Convert the Notification object into a JSON object
request := gorequest.New()
resp, body, errs := request.Post(muto_gcm.GcmAPI).
Set("Content-Type", c.ContentType()).
Set("Authorization", muto_gcm.Authorization).
Send(string(b)).
End()
fmt.Print("Response : %#v %#v %#v", resp, "\n", "Body : ", body, "\n", "Error : ", errs)
} else {
dict := apns.NewAlertDictionary()
dict.Body = combined.Contents.Body
// Including them gave unexpected results initially, but supported features by APNs.
// dict.Title = combined.AlertDict.Title
// dict.TitleLocKey = combined.AlertDict.TitleLocKey
// dict.TitleLocArgs = combined.AlertDict.TitleLocArgs
dict.ActionLocKey = combined.AlertDict.ActionLocKey
dict.LocKey = combined.AlertDict.LocKey
dict.LocArgs = combined.AlertDict.LocArgs
dict.LaunchImage = combined.AlertDict.LaunchImage
payload := apns.NewPayload()
payload.Alert = dict
payload.Badge = combined.Badge
payload.Sound = combined.Sound
payload.ContentAvailable = combined.ContentAvailable
payload.Category = combined.Category
pn := apns.NewPushNotification()
pn.DeviceToken = val.DeviceID
pn.AddPayload(payload)
client := apns.NewClient(muto_gcm.ApnsAPI, "/Users/shivam_mac/Downloads/cert.pem", "/Users/shivam_mac/Downloads/key.pem")
resp := client.Send(pn)
fmt.Println("Response from the APNS Server : ", resp)
alert, _ := pn.PayloadString()
fmt.Println(alert)
}
}
})
fmt.Println("Going live on :9011")
r.Run(":9011")
}