使用Go 送出GCM推播訊息給Android設備

環境準備

go get github.com/google/go-gcm

GCM原理

主程式

package main
import (
       "fmt"
       "github.com/google/go-gcm"
)

func main() {
       SendGMToClient("Hello from GCM","<CLIENT TOKEN>")
}

func SendGMToClient(pushText string,pushToken string){
       serverKey := "<YOUR SERVER KEY>"
       var msg gcm.HttpMessage
       data := map[string]interface{}{"message":pushText}
       regIDs := []string{pushToken}
       msg.RegistrationIds = regIDs
       msg.Data = data
       response,err := gcm.SendHttp(serverKey,msg)
       if err != nil {
fmt.Println(err.Error())
}else{
              fmt.Println("Response ",response.Success)
              fmt.Println("MessageID ",response.MessageId)
              fmt.Println("Failure ",response.Failure)
              fmt.Println("Error ",response.Error)
              fmt.Println("Results ",response.Results)
       }
}
go-gcm document: https:/