Beispiel #1
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)
}
Beispiel #2
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++
	}
}
Beispiel #3
0
func main() {
	var err error

	server := Server{}

	server.mongoSession, err = mgo.DialWithInfo(&mgo.DialInfo{
		Addrs:    []string{"mongodb"},
		Direct:   false,
		FailFast: true,
		Database: "pownyapp",
		Timeout:  10 * time.Second,
	})
	if err != nil {
		log.Fatal("Unable to open MongoDB session: ", err)
	}

	server.mongoSession.SetPoolLimit(128)
	server.mongoSession.SetSyncTimeout(1 * time.Minute)
	server.mongoSession.SetSocketTimeout(1 * time.Minute)
	server.mongoSession.SetSafe(&mgo.Safe{})

	server.mongoSession.DB("").C("messages").EnsureIndex(mgo.Index{
		Key:        []string{"id"},
		Unique:     true,
		Background: true,
		Sparse:     true,
	})
	if err != nil {
		log.Fatal("Unable to create index messages:id: ", err)
	}

	c, err := apns.NewClientWithFiles(apns.SandboxGateway, apnsCert, apnsKey)
	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())
		}
	}()

	server.apnsClient = c

	http.HandleFunc("/powny/ios_token/", func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" {
			server.RegisterIOSTokenHandler(w, r)
			return
		}
		writeError(w, 405, "method not allowed")
	})
	http.HandleFunc("/powny/", func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/powny/" {
			http.NotFound(w, r)
			return
		}
		if r.Method == "GET" {
			server.GetMessagesHandler(w, r)
			return
		}
		if r.Method == "POST" {
			server.PutMessageHandler(w, r)
			return
		}
		if r.Method == "DELETE" {
			server.DeleteMessageHandler(w, r)
			return
		}
		writeError(w, 405, "method not allowed")
	})
	log.Fatal(http.ListenAndServe(":5000", nil))
}
Beispiel #4
0
			})
		})

		Context("valid cert/key pair", func() {
			It("should create a valid client", func() {
				c, err := apns.NewClient(apns.ProductionGateway, DummyCert, DummyKey)
				Expect(err).To(BeNil())
				Expect(c.Conn).NotTo(BeNil())
			})
		})
	})

	Describe(".NewConnWithFiles", func() {
		Context("missing cert/key pair", func() {
			It("should error out", func() {
				_, err := apns.NewClientWithFiles(apns.ProductionGateway, "missing", "missing_also")
				Expect(err).NotTo(BeNil())
			})
		})

		Context("valid cert/key pair", func() {
			var certFile, keyFile *os.File

			BeforeEach(func() {
				certFile, _ = ioutil.TempFile("", "cert.pem")
				certFile.Write([]byte(DummyCert))
				certFile.Close()

				keyFile, _ = ioutil.TempFile("", "key.pem")
				keyFile.Write([]byte(DummyKey))
				keyFile.Close()