2016-05-31 01:02:34 +03:00
|
|
|
# go-fcm
|
2016-06-23 14:53:04 +03:00
|
|
|
[](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=MYW4MY786JXFN&lc=GB&item_name=go%2dfcm%20development&item_number=go%2dfcm¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted)
|
|
|
|
|
|
|
|
|
2016-05-31 01:02:34 +03:00
|
|
|
Firebase Cloud Messaging ( FCM ) Library using golang ( Go )
|
|
|
|
|
|
|
|
This library uses HTTP/JSON Firebase Cloud Messaging connection server protocol
|
2016-05-31 01:03:33 +03:00
|
|
|
|
|
|
|
|
2016-06-22 13:52:42 +03:00
|
|
|
###### features
|
2016-06-21 12:02:51 +03:00
|
|
|
|
|
|
|
* send messages to a topic
|
|
|
|
* send messages to a device list
|
|
|
|
* message can be a notification or data payload
|
|
|
|
|
2016-06-21 12:03:50 +03:00
|
|
|
|
|
|
|
|
2016-06-22 13:52:42 +03:00
|
|
|
###### in progress
|
2016-06-21 12:02:51 +03:00
|
|
|
* retry
|
|
|
|
* instance id features
|
2016-06-21 12:03:50 +03:00
|
|
|
|
2016-06-21 12:02:51 +03:00
|
|
|
|
|
|
|
|
2016-06-22 13:52:42 +03:00
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
2016-06-21 12:02:51 +03:00
|
|
|
|
|
|
|
```
|
|
|
|
go get github.com/NaySoftware/go-fcm
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2016-06-22 13:52:42 +03:00
|
|
|
### Notes
|
|
|
|
|
|
|
|
|
|
|
|
serverKey is the server key by Firebase Cloud Messaging
|
2016-06-22 14:04:39 +03:00
|
|
|
|
|
|
|
Server Key can be found in:
|
|
|
|
|
2016-06-22 14:06:06 +03:00
|
|
|
1. Firebase project settings
|
2016-06-22 14:04:39 +03:00
|
|
|
2. Cloud Messaging
|
|
|
|
3. then copy the server key
|
2016-06-21 12:02:51 +03:00
|
|
|
|
|
|
|
|
2016-06-23 14:53:04 +03:00
|
|
|
|
|
|
|
|
2016-06-22 13:52:42 +03:00
|
|
|
# Examples
|
2016-06-21 12:02:51 +03:00
|
|
|
|
2016-06-22 13:52:42 +03:00
|
|
|
### Send to A topic
|
2016-06-21 12:02:51 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/NaySoftware/go-fcm"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-06-22 13:52:42 +03:00
|
|
|
serverKey = "YOUR-KEY"
|
2016-06-21 12:02:51 +03:00
|
|
|
topic = "/topics/someTopic"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
data := map[string]string{
|
|
|
|
"msg": "Hello World1",
|
|
|
|
"sum": "Happy Day",
|
|
|
|
}
|
|
|
|
|
2016-06-22 13:52:42 +03:00
|
|
|
c := fcm.NewFcmClient(serverKey)
|
2016-06-21 12:02:51 +03:00
|
|
|
c.NewFcmMsgTo(topic, data)
|
|
|
|
|
|
|
|
status, err := c.Send(1)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
status.PrintResults()
|
|
|
|
} else {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2016-06-22 13:52:42 +03:00
|
|
|
### Send to a list of Devices (tokens)
|
2016-06-21 12:02:51 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/NaySoftware/go-fcm"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-06-22 13:52:42 +03:00
|
|
|
serverKey = "YOUR-KEY"
|
2016-06-21 12:02:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
data := map[string]string{
|
|
|
|
"msg": "Hello World1",
|
|
|
|
"sum": "Happy Day",
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := []string{
|
|
|
|
"token1",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xds := []string{
|
|
|
|
"token5",
|
|
|
|
"token6",
|
|
|
|
"token7",
|
|
|
|
}
|
|
|
|
|
2016-06-22 13:52:42 +03:00
|
|
|
c := fcm.NewFcmClient(serverKey)
|
2016-06-21 12:02:51 +03:00
|
|
|
c.NewFcmRegIdsMsg(ids, data)
|
|
|
|
c.AppendDevices(xds)
|
|
|
|
|
|
|
|
status, err := c.Send(1)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
status.PrintResults()
|
|
|
|
} else {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|