From 0bd6840f2864425a19fcae7c121940d2f89e639c Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 9 Apr 2016 12:15:39 +0800 Subject: [PATCH] fix #31 support iOS expiration field Signed-off-by: Bo-Yi Wu --- gorush/notification.go | 22 ++++++++++++++-------- gorush/notification_test.go | 4 ++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/gorush/notification.go b/gorush/notification.go index a0e514f..2861c6e 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -7,6 +7,7 @@ import ( apns "github.com/sideshow/apns2" "github.com/sideshow/apns2/certificate" "github.com/sideshow/apns2/payload" + "time" ) type ExtendJSON struct { @@ -59,14 +60,15 @@ type RequestPushNotification struct { Notification gcm.Notification `json:"notification,omitempty"` // iOS - ApnsID string `json:"apns_id,omitempty"` - Topic string `json:"topic,omitempty"` - Badge int `json:"badge,omitempty"` - Sound string `json:"sound,omitempty"` - Category string `json:"category,omitempty"` - URLArgs []string `json:"url-args,omitempty"` - Extend []ExtendJSON `json:"extend,omitempty"` - Alert Alert `json:"alert,omitempty"` + Expiration int64 `json:"expiration,omitempty"` + ApnsID string `json:"apns_id,omitempty"` + Topic string `json:"topic,omitempty"` + Badge int `json:"badge,omitempty"` + Sound string `json:"sound,omitempty"` + Category string `json:"category,omitempty"` + URLArgs []string `json:"url-args,omitempty"` + Extend []ExtendJSON `json:"extend,omitempty"` + Alert Alert `json:"alert,omitempty"` } func CheckPushConf() error { @@ -141,6 +143,10 @@ func GetIOSNotification(req RequestPushNotification) *apns.Notification { notification.Topic = req.Topic } + if req.Expiration > 0 { + notification.Expiration = time.Unix(req.Expiration, 0) + } + if len(req.Priority) > 0 && req.Priority == "normal" { notification.Priority = apns.PriorityLow } diff --git a/gorush/notification_test.go b/gorush/notification_test.go index ba5f897..0445798 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -8,6 +8,7 @@ import ( "log" "os" "testing" + "time" ) func TestDisabledAndroidIosConf(t *testing.T) { @@ -59,12 +60,14 @@ func TestCorrectConf(t *testing.T) { func TestIOSNotificationStructure(t *testing.T) { var dat map[string]interface{} + var unix = time.Now().Unix() test := "test" message := "Welcome notification Server" req := RequestPushNotification{ ApnsID: test, Topic: test, + Expiration: time.Now().Unix(), Priority: "normal", Message: message, Badge: 1, @@ -106,6 +109,7 @@ func TestIOSNotificationStructure(t *testing.T) { assert.Equal(t, test, notification.ApnsID) assert.Equal(t, test, notification.Topic) + assert.Equal(t, unix, notification.Expiration.Unix()) assert.Equal(t, ApnsPriorityLow, notification.Priority) assert.Equal(t, message, alert) assert.Equal(t, 1, int(badge))