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
}
Exemple #2
0
func Notify(message *ApnsMessage, deviceToken string) {
	payload := apns.NewPayload()
	payload.Alert = message.Message
	payload.Badge = message.Badge
	payload.Sound = message.Sound

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

	apnsClient.Send(pn)
}
Exemple #3
0
func (this *CommandMsg) pushiOS(server *Server) {
	deviceToken, deviceTokenOkay := this.Command["device_token"]
	build, buildOkay := this.Command["build"]

	if !deviceTokenOkay {
		log.Println("Device token not provided!")
		return
	}

	if !buildOkay {
		log.Println("Build type not provided!")
		return
	}

	msg, err := this.formatMessage()
	if err != nil {
		log.Println("Could not format message")
		return
	}

	payload := apns.NewPayload()
	payload.Sound = viper.GetString("ios_push_sound")

	// allow message or message_text to trigger Alert
	if _, messageExists := msg.Data["message"]; messageExists {
		payload.Alert = msg.Data["message"]
	}
	if _, textExists := msg.Data["message_text"]; textExists {
		payload.Alert = msg.Data["message_text"]
	}

	badgeAmt, hasBadge := msg.Data["badge_count"]
	if hasBadge {
		payload.Badge = int(badgeAmt.(float64))
	}

	pn := apns.NewPushNotification()
	pn.DeviceToken = deviceToken
	pn.AddPayload(payload)
	pn.Set("payload", msg)

	client := server.GetAPNSClient(build)
	resp := client.Send(pn)
	alert, _ := pn.PayloadString()
	server.Stats.LogAPNSPush()

	if resp.Error != nil {
		server.Stats.LogAPNSError()
		log.Printf("Alert (iOS): %s\n", alert)
		log.Printf("Error (iOS): %s\n", resp.Error)
	}
}
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)
}
Exemple #5
0
func SendAppleNotify(deviceId string, message string, badge int) *model.AppError {
	payload := apns.NewPayload()
	payload.Alert = message
	payload.Badge = 1

	pn := apns.NewPushNotification()
	pn.DeviceToken = deviceId
	pn.AddPayload(payload)
	client := apns.BareClient(Cfg.EmailSettings.ApplePushServer, Cfg.EmailSettings.ApplePushCertPublic, Cfg.EmailSettings.ApplePushCertPrivate)
	resp := client.Send(pn)

	if resp.Error != nil {
		return model.NewAppError("", "Could not send apple push notification", fmt.Sprintf("id=%v err=%v", deviceId, resp.Error))
	} else {
		return nil
	}
}
Exemple #6
0
func (this *PushHelper) SendApplePush(push RedisPush, token string) error {
	payload := apns.NewPayload()
	payload.Alert = push.Contents
	payload.Badge = 1
	payload.Sound = "default"
	payload.Category = push.Cid

	pn := apns.NewPushNotification()
	pn.DeviceToken = token
	pn.AddPayload(payload)
	resp := this.client.Send(pn)

	alert, _ := pn.PayloadString()
	if resp.Error != nil {
		LogError(fmt.Sprintf("SendApplePush %s err:%v", alert, resp.Error))
	} else {
		LogInfo(fmt.Sprintf("SendApplePush %s success:%v", alert, resp))
	}
	return resp.Error
}
Exemple #7
0
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)
}
Exemple #8
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
}
Exemple #9
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 回调处理

	}
}