Esempio n. 1
0
func (s *Server) sendPushNotification(login string) {
	token, err := s.getIOSToken(login)
	if err != nil {
		log.Print("Failed to get token for ", login, ": ", err)
		return
	}

	p := apns.NewPayload()
	//p.APS.Alert.Body = "Received a new message."
	//badge := 1
	//p.APS.Badge = &badge
	//p.APS.Sound = "1"
	p.APS.Sound = ""
	p.APS.ContentAvailable = 1

	m := apns.NewNotification()
	m.Payload = p
	m.DeviceToken = token
	m.Priority = apns.PriorityImmediate
	//m.Identifier = 12312, // Integer for APNS
	//m.ID = "user_id:timestamp", // ID not sent to Apple – to identify error notifications

	err = s.apnsClient.Send(m)
	log.Print("sent push for ", login, ": ", err)
}
Esempio n. 2
0
func (n *Notification) Send() (errList Notification, err error) {
	// GCM
	apnTask := make(chan bool)
	gcmTask := make(chan bool)
	var (
		apnWG sync.WaitGroup
		gcmWG sync.WaitGroup
	)
	go func() { // parallel between APN and GCM
		// APN
		p := apns.NewPayload()
		p.APS.Alert.Body = n.Message
		p.APS.ContentAvailable = 1
		m := apns.NewNotification()
		m.Payload = p
		m.Priority = apns.PriorityImmediate
		apnWG.Add(len(n.APNReceivers))
		for _, v := range APNReceivers {
			go func() {
				m.DeviceToken = v
				apnClient.Send(m)
				apnWG.Done()
			}() // parallel for APN
		}
		// HERE err handling for APN
		go func() {
			for _, f := range apnClient.FailedNotifs {
				errList.APNReceivers = append(errList.APNReceivers, f.Notif.ID)
			}
		}()
		apnWG.Wait()
		apnTask <- true
	}()
	// GCM
	go func() {
		gcmWG.Add(len(n.GCMReceivers))
		data := map[string]interface{}{"message": n.Message}
		for _, v := range GCMReceivers() {
			go func() {
				d := gcm.NewMessage(data, v)
				_, err := gcmClient.Send(d, 0)
				if err != nil {
					errList.GCMReceivers = append(errList.GCMReceivers, v)
				}
				gcmWG.Done()
			}()
		}
		gcmWG.Wait()
		gcmTask <- true
	}()
	<-apnTask
	<-gcmTask
	return
}
Esempio n. 3
0
// Send PushNotification to device associated with token.
func SendPush(token string, cert string, key string, msg *Message) {
	c, err := apns.NewClientWithFiles(apns.SandboxGateway, cert, key)

	if err != nil {
		log.Fatal("could not create new client", err.Error())
	}

	go func() {
		for f := range c.FailedNotifs {
			fmt.Println("Notif", f.Notif.ID, "failed with", f.Err.Error())
		}
	}()

	p := apns.NewPayload()
	p.APS.Alert.Body = msg.Body
	badge := msg.Badge
	p.APS.Badge = &badge
	p.APS.Sound = msg.Sound
	p.APS.ContentAvailable = 1

	var dat map[string]interface{}

	if err := json.Unmarshal([]byte(msg.Custom), &dat); err != nil {
		fmt.Println(err)
	} else {
		for k, v := range dat {
			p.SetCustomValue(k, valueString(v))
		}
	}

	m := apns.NewNotification()
	m.Payload = p
	m.DeviceToken = token
	m.Priority = apns.PriorityImmediate
	m.Identifier = 12345
	m.ID = "user_id:timestamp"

	c.Send(m)
}
Esempio n. 4
0
func sendNotif(listing *models.Listing, seller *models.User, pending int) {
	var apnsClient apns.Client
	if os.Getenv("ENV") == "production" {
		apnsClient, _ = apns.NewClient(apns.ProductionGateway, os.Getenv("APNS_CERT"), os.Getenv("APNS_KEY"))
	} else {
		apnsClient, _ = apns.NewClient(apns.SandboxGateway, os.Getenv("APNS_CERT"), os.Getenv("APNS_KEY"))
	}

	payload := apns.NewPayload()
	payload.APS.Badge = pending
	payload.APS.Alert.Body = fmt.Sprintf("Someone reserved your %s.", listing.Title)
	payload.APS.Sound = "default"

	for _, device := range seller.Devices {
		notif := apns.NewNotification()
		notif.Payload = payload
		notif.DeviceToken = device
		notif.Priority = apns.PriorityImmediate

		apnsClient.Send(notif)
	}
}
Esempio n. 5
0
func main() {
	c, err := apns.NewClientWithFiles(apns.ProductionGateway, "apns.crt", "apns.key")
	if err != nil {
		log.Fatal("Could not create client", err.Error())
	}

	i := 0
	for {
		fmt.Print("Enter '<token> <badge> <msg>': ")

		var tok, body string
		var badge uint

		_, err := fmt.Scanf("%s %d %s", &tok, &badge, &body)
		if err != nil {
			fmt.Printf("Something went wrong: %v\n", err.Error())
			continue
		}

		p := apns.NewPayload()
		p.APS.Alert.Body = body
		p.APS.Badge.Set(badge)

		p.SetCustomValue("link", "yourapp://precache/20140718")

		m := apns.NewNotification()
		m.Payload = p
		m.DeviceToken = tok
		m.Priority = apns.PriorityImmediate
		m.Identifier = uint32(i)

		c.Send(m)

		i++
	}
}
					p.APS.Alert.Body = "testing"
					p.SetCustomValue("email", "*****@*****.**")

					b, err := json.Marshal(p)

					Expect(err).To(BeNil())
					Expect(b).To(Equal([]byte("{\"aps\":{\"alert\":{\"body\":\"testing\"}},\"email\":\"[email protected]\"}")))
				})
			})
		})
	})

	Describe("Notification", func() {
		Describe("#ToBinary", func() {
			Context("invalid token format", func() {
				n := apns.NewNotification()
				n.DeviceToken = "totally not a valid token"

				It("should return an error", func() {
					_, err := n.ToBinary()
					Expect(err).NotTo(BeNil())
					Expect(err.Error()).To(ContainSubstring("convert token to hex error"))
				})
			})

			Context("valid payload", func() {
				It("should generate the correct byte payload with expiry", func() {
					t := time.Unix(1404102833, 0)

					n := apns.NewNotification()
					n.Identifier = uint32(123123)