Example #1
0
func NewGCMPusher(key string) *GCMPusher {
	pusher := &GCMPusher{
		events:    make(chan Event),
		deviceIds: make(map[string]struct{}),
	}
	if len(key) != 0 {
		pusher.client = gcm.New(key)
	}
	go pusher.loop()

	return pusher
}
Example #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)
		}
	}
}