func sendiOSNotificationToDevice(token string, notification Notification, number int, done chan bool) {

	payload := apns.NewPayload()
	payload.Alert = notification.Message
	payload.Badge = number
	payload.Sound = "bingbong.aiff"

	pn := apns.NewPushNotification()
	pn.DeviceToken = token
	pn.AddPayload(payload)
	pn.Set("id", notification.ID)
	pn.Set("type", notification.Type)
	pn.Set("sender", notification.Sender)
	pn.Set("content", notification.Content)
	pn.Set("message", notification.Message)
	if notification.Type == "tag" {
		pn.Set("comment", notification.Comment.ID)
	}

	config, _ := Configuration()

	if config.Environment == "staging" {
		client := apns.NewClient("gateway.sandbox.push.apple.com:2195", "InsappDevCert.pem", "InsappDev.pem")
		client.Send(pn)
		pn.PayloadString()
	} else {
		client := apns.NewClient("gateway.push.apple.com:2195", "InsappProdCert.pem", "InsappProd.pem")
		client.Send(pn)
		pn.PayloadString()
	}

	done <- true
}
Beispiel #2
0
func NewServer(conf *Configuration, store *Storage) *Server {
	hash := md5.New()
	io.WriteString(hash, time.Now().String())
	id := string(hash.Sum(nil))

	timeout := time.Duration(conf.GetInt("connection_timeout"))

	var runtimeStats RuntimeStats

	if conf.GetBool("datadog_enabled") {
		runtimeStats, _ = NewDatadogStats(conf.Get("datadog_host"))
		runtimeStats.LogStartup()
	} else {
		runtimeStats = &DiscardStats{}
	}

	apnsProvider := func(build string) apns.APNSClient {
		return apns.NewClient(conf.Get("apns_"+build+"_url"), conf.Get("apns_"+build+"_cert"), conf.Get("apns_"+build+"_private_key"))
	}

	gcmProvider := func() GCMClient {
		return &gcm.Sender{ApiKey: conf.Get("gcm_api_key")}
	}

	return &Server{
		ID:           id,
		Config:       conf,
		Store:        store,
		timeout:      timeout,
		Stats:        runtimeStats,
		apnsProvider: apnsProvider,
		gcmProvider:  gcmProvider,
	}
}
Beispiel #3
0
func NewServer(store *Storage, stats RuntimeStats) *Server {
	hash := md5.New()
	io.WriteString(hash, time.Now().String())
	id := string(hash.Sum(nil))

	timeout := time.Duration(viper.GetInt("connection_timeout"))

	if timeout <= 0 {
		panic(fmt.Errorf("connection_timeout <= 0: %+v", timeout))
	}

	apnsProvider := func(build string) apns.APNSClient {
		return apns.NewClient(viper.GetString("apns_"+build+"_url"), viper.GetString("apns_"+build+"_cert"), viper.GetString("apns_"+build+"_private_key"))
	}

	gcmProvider := func() GCMClient {
		return &gcm.Sender{ApiKey: viper.GetString("gcm_api_key")}
	}

	return &Server{
		ID:           id,
		Store:        store,
		timeout:      timeout,
		Stats:        stats,
		apnsProvider: apnsProvider,
		gcmProvider:  gcmProvider,
	}
}
Beispiel #4
0
func NewPush(isproc int) (p *PushHelper) {
	p = new(PushHelper)
	// file, _ := exec.LookPath(os.Args[0])
	// path, _ := filepath.Abs(file)
	// fmt.Println("path", path)
	if isproc == 1 {
		p.ApnsUrl = "gateway.push.apple.com:2195"
		p.client = apns.NewClient(p.ApnsUrl, "/www/bin/job/aps_product_identity.pem", "/www/bin/job/product_key.unencrypted.pem")
	} else if isproc == 2 {
		p.ApnsUrl = "gateway.push.apple.com:2195"
		p.client = apns.NewClient(p.ApnsUrl, "/Users/qihoo/Documents/goagent/workspace/src/qiaoqiao/job/aps_product_identity.pem", "/Users/qihoo/Documents/goagent/workspace/src/qiaoqiao/job/product_key.unencrypted.pem")
	} else {
		p.ApnsUrl = "gateway.sandbox.push.apple.com:2195"
		p.client = apns.NewClient(p.ApnsUrl, "/www/bin/job/aps_developer_identity.pem", "/www/bin/job/key.unencrypted.pem")
		// p.client = apns.NewClient(p.ApnsUrl, "/Users/qihoo/Documents/goagent/workspace/src/qiaoqiao/job/aps_developer_identity.pem", "/Users/qihoo/Documents/goagent/workspace/src/qiaoqiao/job/key.unencrypted.pem")
	}
	return p
}
func sendPushToUser(user string, fromUser string) {
	payload := apns.NewPayload()
	payload.Alert = fromUser + " vous a envoyé un shot 😏📸"
	payload.Badge = 1
	payload.Sound = "bingbong.aiff"

	pn := apns.NewPushNotification()
	pn.DeviceToken = getDeviceTokenForUser(user)
	pn.AddPayload(payload)

	client := apns.NewClient("gateway.push.apple.com:2195", "./Push/Prod/PushChocoshotCert.pem", "./Push/Prod/key.unencrypted.pem")
	client.Send(pn)
}
Beispiel #6
0
Datei: core.go Projekt: r4mp/c3an
func SendNotificationToSingleDevice(message string, badge int, sound string, token string) ([]byte, error) {

	payload := apns.NewPayload()

	payload.Alert = message
	payload.Badge = badge
	payload.Sound = sound

	pn := apns.NewPushNotification()
	pn.DeviceToken = token
	pn.AddPayload(payload)

	client := apns.NewClient("gateway.sandbox.push.apple.com:2195", "certs/cert.pem", "certs/key.pem")
	resp := client.Send(pn)

	alert, _ := pn.PayloadString()

	res := ApnResult{alert, resp.Success, resp.Error}

	return json.Marshal(res)
}
Beispiel #7
0
func (r *sqlRepository) Push(users []string, message string, cert string, key string) error {
	tokens := []*Token{}

	for i := range users {
		users[i] = "'" + users[i] + "'"
	}

	statement := fmt.Springf("SELECT * FROM token WHERE user IN (%s)", strings.Join(users, ", "))
	err := r.db.Select(&tokens, statement)
	if err != nil {
		return err
	}

	payload := apns.NewPayload()
	payload.Alert = message
	payload.Badge = 1 // TODO: Make this more accurate
	payload.Sound = "bingbong.aiff"

	client := apns.NewClient("gateway.push.apple.com:2195", cert, key)

	for _, token := range tokens {
		pn := apns.NewPushNotification()
		pn.DeviceToken = token.Token
		pn.AddPayload(payload)
		resp := client.Send(pn)

		alert, _ := pn.PayloadString()
		if resp.Error != nil {
			log.Println("APNS Error: ", resp.Error)
		} else {
			log.Println("APNS Alert: ", alert)
			log.Println("APNS Success: ", resp.Success)
		}
	}

	return nil
}
Beispiel #8
0
//推送IOS离线消息
func pushAPNS(msg map[string]interface{}, resources []*Resource, apnsToken []ApnsToken) {

	var host = "gateway.sandbox.push.apple.com:2195"
	if Conf.ApnsType == "product" {
		host = "gateway.push.apple.com:2195"
	}

	var certFile = ""
	var keyFile = ""
	for _, r := range resources {
		if r.Type == "0" {
			certFile = r.Content
		} else if r.Type == "1" {
			keyFile = r.Content
		}
	}

	if certFile == "" || keyFile == "" {
		logger.Errorf("Push message failed. CertFile [%v] or KeyFile[%v] has a error ", certFile, keyFile)
		return
	}

	for _, t := range apnsToken {
		content := msg["content"].(string)
		toUserName := msg["toUserName"].(string)

		if strings.HasSuffix(toUserName, QUN_SUFFIX) {
			if strings.Contains(content, "&&") {
				content = strings.Split(content, "&&")[1]
			}
		}
		contentMsg := msg["fromDisplayName"].(string) + ":" + content
		logger.Infof(" contentMsg[%v] ", contentMsg)

		if len(contentMsg) > 256 {
			contentMsg = substr(contentMsg, 0, 250) + "..."
		}

		payload := apns.NewPayload()
		payload.Alert = contentMsg
		payload.Badge = 1
		payload.Sound = "bingbong.aiff"
		payload.Category = "Test!"
		payload.ContentAvailable = 1

		pn := apns.NewPushNotification()
		pn.DeviceToken = t.ApnsToken
		pn.AddPayload(payload)
		if nil != msg["customFilelds"] {
			customFilelds := msg["customFilelds"].(map[string]interface{})
			for key, value := range customFilelds {
				pn.Set(key, value)
			}

		}

		client := apns.NewClient(host, certFile, keyFile)
		resp := client.Send(pn)
		alert, _ := pn.PayloadString()
		if !resp.Success {
			logger.Errorf("Push message failed. ApnsToken[%v],Content[%v],Error[%v],Host[%v],CertFile [%v], KeyFile[%v]", t.ApnsToken, alert, resp.Error, host, certFile, keyFile)
			// 推送分发过程中失败不立即返回,继续下一个推送

			//只删除失效类型
			if resp.Error.Error() == apns.ApplePushResponses[8] || resp.Error.Error() == apns.ApplePushResponses[5] {
				if deleteApnsToken(t.ApnsToken) {
					logger.Trace("delete INVALID_TOKEN  succeed")
				} else {
					logger.Trace("delete  INVALID_TOKEN failure")
				}
			}

		} else {
			logger.Infof("Push message successed. ApnsToken[%v],Content[%v],Host[%v]", t.ApnsToken, alert, host)
		}
		// TODO: APNs 回调处理

	}
}
Beispiel #9
0
func init() {
	var apnsCertFile = os.Getenv("APNS_CERT")
	var apnsKeyfile = os.Getenv("APNS_KEY")
	apnsClient = apns.NewClient("gateway.sandbox.push.apple.com:2195", apnsCertFile, apnsKeyfile)
}