Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/json"
- "flag"
- "fmt"
- "log"
- "os"
- "strings"
- "github.com/pubnub/go/messaging"
- "github.com/sideshow/apns2"
- "github.com/sideshow/apns2/certificate"
- "github.com/sideshow/apns2/payload"
- )
- // type notification []byte
- type message struct {
- Notification notification `json:"notification"`
- }
- type notification struct {
- ID string `json:"id"`
- Code string `json:"code"`
- Subject string `json:"subject"`
- Message string `json:"message"`
- Foreground bool `json:"foreground"`
- }
- var publishKey = "pub-c-032da06b-f98a-4f34-9e8b-251643103983"
- var subscribeKey = "sub-c-2cee0384-555f-11e3-af9c-02ee2ddab7fe"
- func listenPubnubNotification(channel, device string, notifications chan<- notification) {
- pubnub := messaging.NewPubnub(publishKey, subscribeKey, "", "", true, "")
- successChannel := make(chan []byte)
- errorChannel := make(chan []byte)
- go pubnub.Subscribe(channel, "", successChannel, false, errorChannel)
- for {
- select {
- case content := <-successChannel:
- var pubnubMessage []interface{}
- json.Unmarshal(content, &pubnubMessage)
- switch t := pubnubMessage[0].(type) {
- case float64:
- if strings.Contains(pubnubMessage[1].(string), "connected") {
- fmt.Println("connected")
- }
- case []interface{}:
- messages, _ := json.Marshal(pubnubMessage[0].([]interface{}))
- var notes []message
- err := json.Unmarshal(messages, ¬es)
- if err != nil {
- log.Fatal(err)
- }
- for _, note := range notes {
- if note.Notification.Subject == "" {
- continue
- }
- notifications <- note.Notification
- }
- default:
- panic(fmt.Sprintf("Unknown type: %T", t))
- }
- case err := <-errorChannel:
- fmt.Println(err)
- fmt.Println("pubnub failed")
- os.Exit(-1)
- }
- }
- }
- func sendNotifications(device string, notificationChannel <-chan notification) {
- cert, err := certificate.FromPemFile("./certificate.pem", "")
- if err != nil {
- log.Fatal(err)
- os.Exit(1)
- }
- client := apns2.NewClient(cert).Development()
- for {
- select {
- case message := <-notificationChannel:
- notification := &apns2.Notification{}
- notification.DeviceToken = device
- notification.Topic = "com.heetch.HeetchTest"
- notification.Payload = payload.NewPayload().AlertTitle(message.Subject).AlertBody(message.Message).Sound("default")
- fmt.Println("will push")
- res, err := client.Push(notification)
- if err != nil {
- log.Fatal("Error:", err)
- } else {
- fmt.Println("Notified on reception of: ", message)
- if res.Sent() {
- log.Println("Sent:", res.ApnsID)
- } else {
- fmt.Printf("Not Sent: %v %v %v\n", res.StatusCode, res.ApnsID, res.Reason)
- }
- }
- }
- }
- }
- func main() {
- channelPtr := flag.String("channel", "", "pubnub channel to listen on")
- devicePtr := flag.String("device", "", "iOS device token to publish notifications to")
- flag.Parse()
- channel := *channelPtr
- device := *devicePtr
- if channel == "" || device == "" {
- fmt.Println("-channel [channel] and -device [device] are required")
- os.Exit(-1)
- }
- notifications := make(chan notification)
- go sendNotifications(device, notifications)
- listenPubnubNotification(channel, device, notifications)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement