Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "log"
  8. "os"
  9. "strings"
  10.  
  11. "github.com/pubnub/go/messaging"
  12. "github.com/sideshow/apns2"
  13. "github.com/sideshow/apns2/certificate"
  14. "github.com/sideshow/apns2/payload"
  15. )
  16.  
  17. // type notification []byte
  18.  
  19. type message struct {
  20. Notification notification `json:"notification"`
  21. }
  22.  
  23. type notification struct {
  24. ID string `json:"id"`
  25. Code string `json:"code"`
  26. Subject string `json:"subject"`
  27. Message string `json:"message"`
  28. Foreground bool `json:"foreground"`
  29. }
  30.  
  31. var publishKey = "pub-c-032da06b-f98a-4f34-9e8b-251643103983"
  32. var subscribeKey = "sub-c-2cee0384-555f-11e3-af9c-02ee2ddab7fe"
  33.  
  34. func listenPubnubNotification(channel, device string, notifications chan<- notification) {
  35. pubnub := messaging.NewPubnub(publishKey, subscribeKey, "", "", true, "")
  36.  
  37. successChannel := make(chan []byte)
  38. errorChannel := make(chan []byte)
  39.  
  40. go pubnub.Subscribe(channel, "", successChannel, false, errorChannel)
  41.  
  42. for {
  43. select {
  44. case content := <-successChannel:
  45. var pubnubMessage []interface{}
  46.  
  47. json.Unmarshal(content, &pubnubMessage)
  48. switch t := pubnubMessage[0].(type) {
  49. case float64:
  50. if strings.Contains(pubnubMessage[1].(string), "connected") {
  51. fmt.Println("connected")
  52. }
  53. case []interface{}:
  54.  
  55. messages, _ := json.Marshal(pubnubMessage[0].([]interface{}))
  56.  
  57. var notes []message
  58.  
  59. err := json.Unmarshal(messages, &notes)
  60.  
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64.  
  65. for _, note := range notes {
  66. if note.Notification.Subject == "" {
  67. continue
  68. }
  69. notifications <- note.Notification
  70. }
  71.  
  72. default:
  73. panic(fmt.Sprintf("Unknown type: %T", t))
  74. }
  75.  
  76. case err := <-errorChannel:
  77. fmt.Println(err)
  78. fmt.Println("pubnub failed")
  79. os.Exit(-1)
  80. }
  81. }
  82. }
  83.  
  84. func sendNotifications(device string, notificationChannel <-chan notification) {
  85. cert, err := certificate.FromPemFile("./certificate.pem", "")
  86.  
  87. if err != nil {
  88. log.Fatal(err)
  89. os.Exit(1)
  90. }
  91.  
  92. client := apns2.NewClient(cert).Development()
  93.  
  94. for {
  95. select {
  96. case message := <-notificationChannel:
  97. notification := &apns2.Notification{}
  98. notification.DeviceToken = device
  99. notification.Topic = "com.heetch.HeetchTest"
  100. notification.Payload = payload.NewPayload().AlertTitle(message.Subject).AlertBody(message.Message).Sound("default")
  101.  
  102. fmt.Println("will push")
  103. res, err := client.Push(notification)
  104. if err != nil {
  105. log.Fatal("Error:", err)
  106. } else {
  107. fmt.Println("Notified on reception of: ", message)
  108. if res.Sent() {
  109. log.Println("Sent:", res.ApnsID)
  110. } else {
  111. fmt.Printf("Not Sent: %v %v %v\n", res.StatusCode, res.ApnsID, res.Reason)
  112. }
  113. }
  114. }
  115. }
  116. }
  117.  
  118. func main() {
  119. channelPtr := flag.String("channel", "", "pubnub channel to listen on")
  120. devicePtr := flag.String("device", "", "iOS device token to publish notifications to")
  121.  
  122. flag.Parse()
  123.  
  124. channel := *channelPtr
  125. device := *devicePtr
  126.  
  127. if channel == "" || device == "" {
  128. fmt.Println("-channel [channel] and -device [device] are required")
  129. os.Exit(-1)
  130. }
  131.  
  132. notifications := make(chan notification)
  133.  
  134. go sendNotifications(device, notifications)
  135. listenPubnubNotification(channel, device, notifications)
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement