Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade gcm to fcm #231

Merged
merged 6 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ A push notification micro server using [Gin](https://github.com/gin-gonic/gin) f

## Support Platform

* [APNS](https://developer.apple.com/library/ios/documentation/networkinginternet/conceptual/remotenotificationspg/Chapters/ApplePushService.html)
* [GCM](https://developer.android.com/google/gcm/index.html)
* [APNS](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html)
* [FCM](https://firebase.google.com/)

## Features

* Support [Google Cloud Message](https://developers.google.com/cloud-messaging/) using [go-gcm](https://github.com/google/go-gcm) library for Android.
* Support [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging) using [fcm](https://github.com/edganiukov/fcm) library for Android.
* Support [HTTP/2](https://http2.github.io/) Apple Push Notification Service using [apns2](https://github.com/sideshow/apns2) library.
* Support [YAML](https://github.com/go-yaml/yaml) configuration.
* Support command line to send single Android or iOS notification.
Expand All @@ -56,7 +56,7 @@ A push notification micro server using [Gin](https://github.com/gin-gonic/gin) f
* Support store app stat to memory, [Redis](http://redis.io/), [BoltDB](https://github.com/boltdb/bolt), [BuntDB](https://github.com/tidwall/buntdb) or [LevelDB](https://github.com/syndtr/goleveldb).
* Support `p12` or `pem` formtat of iOS certificate file.
* Support `/sys/stats` show response time, status code count, etc.
* Support for HTTP proxy to Google server (GCM).
* Support for HTTP proxy to Google server (FCM).
* Support retry send notification if server response is fail.
* Support expose [prometheus](https://prometheus.io/) metrics.
* Support install TLS certificates from [Let's Encrypt](https://letsencrypt.org/) automatically.
Expand All @@ -75,7 +75,7 @@ core:
ssl: false
cert_path: "cert.pem"
key_path: "key.pem"
http_proxy: "" # only working for GCM server
http_proxy: "" # only working for FCM server
pid:
enabled: false
path: "gorush.pid"
Expand Down Expand Up @@ -190,7 +190,7 @@ Server Options:
-m, --message <message> Notification message
-t, --token <token> Notification token
--title <title> Notification title
--proxy <proxy> Proxy URL (only for GCM)
--proxy <proxy> Proxy URL (only for FCM)
--pid <pid path> Process identifier path
iOS Options:
-i, --key <file> certificate key file path
Expand All @@ -215,10 +215,10 @@ $ gorush -android -m="your message" -k="API Key" -t="Device token"
```

* `-m`: Notification message.
* `-k`: [Google Cloud Messaging](https://developers.google.com/cloud-messaging) api key
* `-k`: [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging) api key
* `-t`: Device token.
* `--title`: Notification title.
* `--proxy`: Set http proxy url. (only working for GCM)
* `--proxy`: Set http proxy url. (only working for FCM)

### Send iOS notification

Expand Down Expand Up @@ -430,10 +430,10 @@ Request body must has a notifications array. The following is a parameter table
| to | string | The value must be a registration token, notification key, or topic. | - | only Android |
| collapse_key | string | a key for collapsing notifications | - | only Android |
| delay_while_idle | bool | a flag for device idling | - | only Android |
| time_to_live | uint | expiration of message kept on GCM storage | - | only Android |
| time_to_live | uint | expiration of message kept on FCM storage | - | only Android |
| restricted_package_name | string | the package name of the application | - | only Android |
| dry_run | bool | allows developers to test a request without actually sending a message | - | only Android |
| notification | string array | payload of a GCM message | - | only Android. See the [detail](#android-notification-payload) |
| notification | string array | payload of a FCM message | - | only Android. See the [detail](#android-notification-payload) |
| expiration | int | expiration for notification | - | only iOS |
| apns_id | string | A canonical UUID that identifies the notification | - | only iOS |
| topic | string | topic of the remote notification | - | only iOS |
Expand All @@ -457,7 +457,7 @@ Request body must has a notifications array. The following is a parameter table
| title-loc-args | array of strings | Variable string values to appear in place of the format specifiers in title-loc-key. | - | |
| title-loc-key | string | The key to a title string in the Localizable.strings file for the current localization. | - | |

See more detail about [APNs Remote Notification Payload](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW1).
See more detail about [APNs Remote Notification Payload](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html).

### Android notification payload

Expand All @@ -472,7 +472,7 @@ See more detail about [APNs Remote Notification Payload](https://developer.apple
| title_loc_key | string | Indicates the key to the title string for localization. | - | |
| title_loc_args | string | Indicates the string value to replace format specifiers in title string for localization. | - | |

See more detail about [GCM server reference](https://developers.google.com/cloud-messaging/http-server-ref#send-downstream).
See more detail about [Firebase Cloud Messaging HTTP Protocol reference](https://firebase.google.com/docs/cloud-messaging/http-server-ref#send-downstream).

### iOS Example

Expand Down
2 changes: 1 addition & 1 deletion config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ core:
ssl: false
cert_path: "cert.pem"
key_path: "key.pem"
http_proxy: "" # only working for GCM server
http_proxy: "" # only working for FCM server
pid:
enabled: false
path: "gorush.pid"
Expand Down
30 changes: 18 additions & 12 deletions gorush/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"sync"
"time"

"github.com/google/go-gcm"
"github.com/edganiukov/fcm"
apns "github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate"
"github.com/sideshow/apns2/payload"
Expand Down Expand Up @@ -75,7 +75,7 @@ type PushNotification struct {
TimeToLive *uint `json:"time_to_live,omitempty"`
RestrictedPackageName string `json:"restricted_package_name,omitempty"`
DryRun bool `json:"dry_run,omitempty"`
Notification gcm.Notification `json:"notification,omitempty"`
Notification fcm.Notification `json:"notification,omitempty"`

// iOS
Expiration int64 `json:"expiration,omitempty"`
Expand Down Expand Up @@ -142,7 +142,7 @@ func CheckMessage(req PushNotification) error {
return nil
}

// SetProxy only working for GCM server.
// SetProxy only working for FCM server.
func SetProxy(proxy string) error {

proxyURL, err := url.ParseRequestURI(proxy)
Expand Down Expand Up @@ -457,8 +457,8 @@ Retry:
// GetAndroidNotification use for define Android notification.
// HTTP Connection Server Reference for Android
// https://developers.google.com/cloud-messaging/http-server-ref
func GetAndroidNotification(req PushNotification) gcm.HttpMessage {
notification := gcm.HttpMessage{
func GetAndroidNotification(req PushNotification) *fcm.Message {
notification := &fcm.Message{
To: req.To,
CollapseKey: req.CollapseKey,
ContentAvailable: req.ContentAvailable,
Expand All @@ -468,7 +468,7 @@ func GetAndroidNotification(req PushNotification) gcm.HttpMessage {
DryRun: req.DryRun,
}

notification.RegistrationIds = req.Tokens
notification.RegistrationIDs = req.Tokens

if len(req.Priority) > 0 && req.Priority == "high" {
notification.Priority = "high"
Expand Down Expand Up @@ -530,11 +530,17 @@ Retry:
APIKey = req.APIKey
}

res, err := gcm.SendHttp(APIKey, notification)
client, err := fcm.NewClient(APIKey)
if err != nil {
// FCM server error
LogError.Error("FCM server error: " + err.Error())
return false
}

res, err := client.Send(notification)
if err != nil {
// GCM server error
LogError.Error("GCM server error: " + err.Error())
// FCM server error
LogError.Error("FCM server error: " + err.Error())
return false
}

Expand All @@ -544,12 +550,12 @@ Retry:

var newTokens []string
for k, result := range res.Results {
if result.Error != "" {
if result.Error != nil {
isError = true
newTokens = append(newTokens, req.Tokens[k])
LogPush(FailedPush, req.Tokens[k], req, errors.New(result.Error))
LogPush(FailedPush, req.Tokens[k], req, result.Error)
if PushConf.Core.Sync {
req.AddLog(getLogPushEntry(FailedPush, req.Tokens[k], req, errors.New(result.Error)))
req.AddLog(getLogPushEntry(FailedPush, req.Tokens[k], req, result.Error))
}
continue
}
Expand Down
6 changes: 3 additions & 3 deletions gorush/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/appleboy/gorush/config"
"github.com/buger/jsonparser"
"github.com/google/go-gcm"
"github.com/edganiukov/fcm"
"github.com/sideshow/apns2"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -393,7 +393,7 @@ func TestAndroidNotificationStructure(t *testing.T) {
"a": "1",
"b": 2,
},
Notification: gcm.Notification{
Notification: fcm.Notification{
Color: test,
Tag: test,
},
Expand Down Expand Up @@ -709,7 +709,7 @@ func TestAPNSClientProdHost(t *testing.T) {
assert.Equal(t, apns2.HostProduction, ApnsClient.Host)
}

func TestGCMMessage(t *testing.T) {
func TestFCMMessage(t *testing.T) {
var req PushNotification
var err error

Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/edganiukov/fcm/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions vendor/github.com/edganiukov/fcm/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading