go-fcm/README.md

153 lines
2.6 KiB
Markdown
Raw Normal View History

2016-07-10 18:23:54 +03:00
# go-fcm : FCM Library for Go
2016-06-23 14:53:04 +03:00
2016-06-23 16:10:05 +03:00
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg?style=flat-square)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=MYW4MY786JXFN&lc=GB&item_name=go%2dfcm%20development&item_number=go%2dfcm&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted)
2016-07-10 18:23:54 +03:00
[![AUR](https://img.shields.io/aur/license/yaourt.svg?style=flat-square)](https://github.com/NaySoftware/go-fcm/blob/master/LICENSE)
2016-06-23 14:53:04 +03:00
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-07-10 18:23:54 +03:00
###### Features
2016-06-21 12:02:51 +03:00
2016-06-23 18:48:40 +03:00
* Send messages to a topic
* Send messages to a device list
* Message can be a notification or data payload
2016-07-10 18:23:54 +03:00
* Supports condition attribute (fcm only)
* Instace Id Features
- Get info about app Instance
- Subscribe app Instance to a topic
- Batch Subscribe/Unsubscribe to/from a topic
- Create registration tokens for APNs tokens
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-07-10 23:17:11 +03:00
## Docs
```
https://godoc.org/github.com/NaySoftware/go-fcm
```
2016-06-22 13:52:42 +03:00
### Notes
2016-07-10 18:23:54 +03:00
###### Server Key
2016-06-22 13:52:42 +03:00
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-07-10 18:23:54 +03:00
###### Retry mechanism
Retry should be implemented based on the requirements.
Sending a request will result with a "FcmResponseStatus" struct, which holds
a detailed information based on the Firebase Response, with RetryAfter
(response header) if available - with a failed request.
its recommended to use a backoff time to retry the request - (if RetryAfter
header is not available).
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
2016-07-10 20:06:17 +03:00
```go
2016-06-21 12:02:51 +03:00
package main
import (
"fmt"
2016-07-10 18:23:54 +03:00
"github.com/NaySoftware/go-fcm"
2016-06-21 12:02:51 +03:00
)
const (
2016-06-22 13:52:42 +03:00
serverKey = "YOUR-KEY"
2016-07-10 18:23:54 +03:00
topic = "/topics/someTopic"
2016-06-21 12:02:51 +03:00
)
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)
2016-06-23 16:10:05 +03:00
2016-07-10 18:23:54 +03:00
status, err := c.Send()
2016-06-21 12:02:51 +03:00
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
2016-07-10 20:06:17 +03:00
```go
2016-06-21 12:02:51 +03:00
package main
import (
"fmt"
2016-07-10 18:23:54 +03:00
"github.com/NaySoftware/go-fcm"
2016-06-21 12:02:51 +03:00
)
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-07-10 18:23:54 +03:00
c.NewFcmRegIdsMsg(ids, data)
c.AppendDevices(xds)
status, err := c.Send()
2016-06-21 12:02:51 +03:00
if err == nil {
status.PrintResults()
} else {
fmt.Println(err)
}
}
```