Send messages to topics for android (#296)

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu
2017-10-24 04:00:08 -05:00
committed by GitHub
parent 4df24202cf
commit 115ee18560
9 changed files with 194 additions and 25 deletions

View File

@@ -1,8 +1,8 @@
# go-fcm
[![GoDoc](https://godoc.org/github.com/edganiukov/fcm?status.svg)](https://godoc.org/github.com/edganiukov/fcm)
[![Build Status](https://travis-ci.org/edganiukov/fcm.svg?branch=master)](https://travis-ci.org/edganiukov/fcm)
[![Go Report Card](https://goreportcard.com/badge/github.com/edganiukov/fcm)](https://goreportcard.com/report/github.com/edganiukov/fcm)
[![GoDoc](https://godoc.org/github.com/appleboy/go-fcm?status.svg)](https://godoc.org/github.com/edganiukov/fcm)
[![Build Status](https://travis-ci.org/appleboy/go-fcm.svg?branch=master)](https://travis-ci.org/edganiukov/fcm)
[![Go Report Card](https://goreportcard.com/badge/github.com/appleboy/go-fcm)](https://goreportcard.com/report/github.com/edganiukov/fcm)
This project was forked from [github.com/edganiukov/fcmfcm](https://github.com/edganiukov/fcm).
@@ -10,6 +10,12 @@ Golang client library for Firebase Cloud Messaging. Implemented only [HTTP clien
More information on [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/)
## Feature
* [x] Send messages to a topic
* [x] Send messages to a device list
* [x] Supports condition attribute (fcm only)
## Getting Started
To install fcm, use `go get`:
@@ -34,31 +40,32 @@ Here is a simple example illustrating how to use FCM library:
package main
import (
"github.com/edganiukov/fcm"
"log"
"github.com/appleboy/go-fcm"
)
func main() {
// Create the message to be sent.
msg := &fcm.Message{
Token: "sample_device_token",
Data: map[string]interface{}{
"foo": "bar",
}
}
To: "sample_device_token",
Data: map[string]interface{}{
"foo": "bar",
},
}
// Create a FCM client to send the message.
client := fcm.NewClient("sample_api_key")
client, err := fcm.NewClient("sample_api_key")
if err != nil {
log.Fatalln(err)
}
// Send the message and receive the response without retries.
response, err := client.Send(msg)
if err != nil {
/* ... */
log.Fatalln(err)
}
/* ... */
log.Printf("%#v\n", response)
}
```
#### TODO:
---------
- [ ] Retry only failed messages while multicast messaging.

View File

@@ -46,6 +46,9 @@ var (
// ErrTopicsMessageRateExceeded occurs when client sent to many requests to
// the topics.
ErrTopicsMessageRateExceeded = errors.New("topics message rate exceeded")
// ErrInvalidParameters occurs when provided parameters have the right name and type
ErrInvalidParameters = errors.New("check that the provided parameters have the right name and type")
)
var (
@@ -62,6 +65,7 @@ var (
"InternalServerError": ErrInternalServerError,
"DeviceMessageRateExceeded": ErrDeviceMessageRateExceeded,
"TopicsMessageRateExceeded": ErrTopicsMessageRateExceeded,
"InvalidParameters": ErrInvalidParameters,
}
)
@@ -105,6 +109,47 @@ type Response struct {
Failure int `json:"failure"`
CanonicalIDs int `json:"canonical_ids"`
Results []Result `json:"results"`
// Device Group HTTP Response
FailedRegistrationIDs []string `json:"failed_registration_ids"`
// Topic HTTP response
MessageID int64 `json:"message_id"`
Error error `json:"error"`
}
// UnmarshalJSON implements json.Unmarshaler interface.
func (r *Response) UnmarshalJSON(data []byte) error {
var response struct {
MulticastID int64 `json:"multicast_id"`
Success int `json:"success"`
Failure int `json:"failure"`
CanonicalIDs int `json:"canonical_ids"`
Results []Result `json:"results"`
// Device Group HTTP Response
FailedRegistrationIDs []string `json:"failed_registration_ids"`
// Topic HTTP response
MessageID int64 `json:"message_id"`
Error string `json:"error"`
}
if err := json.Unmarshal(data, &response); err != nil {
return err
}
r.MulticastID = response.MulticastID
r.Success = response.Success
r.Failure = response.Failure
r.CanonicalIDs = response.CanonicalIDs
r.Results = response.Results
r.Success = response.Success
r.FailedRegistrationIDs = response.FailedRegistrationIDs
r.MessageID = response.MessageID
r.Error = errMap[response.Error]
return nil
}
// Result represents the status of a processed message.

6
vendor/vendor.json vendored
View File

@@ -3,10 +3,10 @@
"ignore": "test",
"package": [
{
"checksumSHA1": "DuVew6znkXuUG1wjGQF+pUyXrHU=",
"checksumSHA1": "TVTjsXflagrWbTXmbxPJdCtTFRo=",
"path": "github.com/appleboy/go-fcm",
"revision": "5f2cb2866531e4e37c7a9ff5fb7a1536e1ffc566",
"revisionTime": "2017-06-01T07:42:50Z"
"revision": "c12f9e2e95b14802da2b4d3807dd12ef0dd80a42",
"revisionTime": "2017-10-24T08:00:40Z"
},
{
"checksumSHA1": "Ab7MUtqX0iq2PUzzBxWpgzPSydw=",