Esempio n. 1
0
func (p *GCMPusher) loop() {
	for event := range p.events {
		deviceIds := p.DeviceIds()
		if len(deviceIds) == 0 || p.client == nil {
			log.Printf("Bypassing GCM push: %v", event)
			continue
		}
		message := gcm.NewMessage(deviceIds...)
		payload, err := json.Marshal(event.Payload)
		if err != nil {
			log.Printf("Failed GCM push: %s", err)
			continue
		}
		message.SetPayload(event.Name, string(payload))
		_, err = p.client.Send(message)
		if err != nil {
			log.Printf("Failed GCM push: %s", err)
			continue
		}
		log.Printf("GCMPushed: %v to %v", event, deviceIds)
	}
}
Esempio n. 2
0
func (m *PushManager) SendGCM(userId string, pushTokens []string, payload *Packet) {
	c := gcm.New(common.Config.GCMAPIKey)
	msg := gcm.NewMessage(pushTokens...)
	switch body := payload.body.(type) {
	case SendPushLog:
		msg.CollapseKey = fmt.Sprintf("%d:%s", body.Log.ServerId, body.Log.Channel)
	}

	var err error
	if payload.RawData == nil {
		payload.RawData, err = json.Marshal(payload.body)
		if err != nil {
			log.Println("SendGCM Error: ", err)
			return
		}
	}

	msg.SetPayload("type", payload.Cmd)
	msg.SetPayload("data", string(payload.RawData))
	resp, err2 := c.Send(msg)
	if err2 != nil {
		log.Println("SendGCM Error: ", err2)
	}
	log.Printf("%+v", resp)
	var removeTokens []string
	for i, result := range resp.Results {
		switch result.Error {
		case "NotRegistered":
			removeTokens = append(removeTokens, "gcm:"+pushTokens[i])
		}
	}
	if removeTokens != nil {
		err = common.RedisSliceRemove(m.PushTokenListKey(userId), &removeTokens)
		if err != nil {
			log.Println("SendGCM - RemoveTokens Error: ", err2)
		}
	}
}