diff --git a/SPEC.md b/SPEC.md index 3369280..4a4e210 100644 --- a/SPEC.md +++ b/SPEC.md @@ -62,12 +62,13 @@ The request-body must have the `notifications` array. Table below shows the para |sound |string |sound type |- | |only iOS | |expiry |int |expiration for notification |- |0 |only iOS. | |content_available|bool |indicate that new content is available |- |false |only iOS. | -|mutable_content |bool |enable Notification Service app extension|- |false |only iOS(10.0+). | +|mutable_content |bool |enable Notification Service app extension|- |false |only iOS(10.0+) | |collapse_key |string |the key for collapsing notifications |- | |only Android | |delay_while_idle |bool |the flag for device idling |- |false |only Android | |time_to_live |int |expiration of message kept on FCM storage|- |0 |only Android | |extend |string array|extensible partition |- | | | |identifier |string |notification identifier |- | |an optional value to identify notification| +|push_type |string |apns-push-type |- |alert |only iOS(13.0+) | The JSON below is the response-body example from Gaurun. In this case, the status is 200(OK). diff --git a/gaurun/apns_http2.go b/gaurun/apns_http2.go index 056d714..91eb336 100644 --- a/gaurun/apns_http2.go +++ b/gaurun/apns_http2.go @@ -121,8 +121,19 @@ func NewApnsPayloadHttp2(req *RequestGaurunNotification) map[string]interface{} } func NewApnsHeadersHttp2(req *RequestGaurunNotification) *push.Headers { + var pushType push.PushType + + // Required when delivering notifications to devices running iOS 13 and later, or watchOS 6 and later. Ignored on earlier system versions. + // cf: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns + if req.PushType == ApnsPushTypeBackground { + pushType = push.PushTypeBackground + } else { + pushType = push.PushTypeAlert + } + headers := &push.Headers{ - Topic: ConfGaurun.Ios.Topic, + Topic: ConfGaurun.Ios.Topic, + PushType: pushType, } if req.Expiry > 0 { diff --git a/gaurun/apns_http2_test.go b/gaurun/apns_http2_test.go new file mode 100644 index 0000000..e685c66 --- /dev/null +++ b/gaurun/apns_http2_test.go @@ -0,0 +1,22 @@ +package gaurun + +import ( + "testing" + + "github.com/RobotsAndPencils/buford/push" + "github.com/stretchr/testify/assert" +) + +func TestNewApnsClientHttp2(t *testing.T) { + req := &RequestGaurunNotification{} + headers := NewApnsHeadersHttp2(req) + assert.Equal(t, push.PushTypeAlert, headers.PushType) + + req = &RequestGaurunNotification{PushType: ApnsPushTypeAlert} + headers = NewApnsHeadersHttp2(req) + assert.Equal(t, push.PushTypeAlert, headers.PushType) + + req = &RequestGaurunNotification{PushType: ApnsPushTypeBackground} + headers = NewApnsHeadersHttp2(req) + assert.Equal(t, push.PushTypeBackground, headers.PushType) +} diff --git a/gaurun/const.go b/gaurun/const.go index 1501ae6..7bf9a59 100644 --- a/gaurun/const.go +++ b/gaurun/const.go @@ -15,3 +15,8 @@ const ( StatusFailedPush = "failed-push" StatusDisabledPush = "disabled-push" ) + +const ( + ApnsPushTypeAlert = "alert" + ApnsPushTypeBackground = "background" +) diff --git a/gaurun/notification.go b/gaurun/notification.go index 5f47b9b..807b13d 100644 --- a/gaurun/notification.go +++ b/gaurun/notification.go @@ -32,6 +32,7 @@ type RequestGaurunNotification struct { // iOS Title string `json:"title,omitempty"` Subtitle string `json:"subtitle,omitempty"` + PushType string `json:"push_type,omitempty"` Badge int `json:"badge,omitempty"` Category string `json:"category,omitempty"` Sound string `json:"sound,omitempty"` @@ -168,6 +169,12 @@ func validateNotification(notification *RequestGaurunNotification) error { return errors.New("empty message") } + if notification.PushType != "" { + if notification.PushType != ApnsPushTypeAlert && notification.PushType != ApnsPushTypeBackground { + return fmt.Errorf("push_type must be %s or %s", ApnsPushTypeAlert, ApnsPushTypeBackground) + } + } + return nil } diff --git a/gaurun/notification_test.go b/gaurun/notification_test.go index ee48bdf..fb9665d 100644 --- a/gaurun/notification_test.go +++ b/gaurun/notification_test.go @@ -41,6 +41,24 @@ func TestValidateNotification(t *testing.T) { }, nil, }, + { + RequestGaurunNotification{ + Tokens: []string{"test token"}, + Platform: 1, + Message: "test message with identifier", + PushType: "alert", + }, + nil, + }, + { + RequestGaurunNotification{ + Tokens: []string{"test token"}, + Platform: 1, + Message: "test message with identifier", + PushType: "background", + }, + nil, + }, // negative cases { @@ -64,6 +82,15 @@ func TestValidateNotification(t *testing.T) { }, errors.New("empty message"), }, + { + RequestGaurunNotification{ + Tokens: []string{"test token"}, + Platform: 1, + Message: "test message with identifier", + PushType: "notpushtype", + }, + errors.New("push_type must be alert or background"), + }, } for _, c := range cases { diff --git a/go.mod b/go.mod index 0992434..870a8f9 100644 --- a/go.mod +++ b/go.mod @@ -2,14 +2,18 @@ module github.com/mercari/gaurun require ( github.com/BurntSushi/toml v0.2.0 - github.com/RobotsAndPencils/buford v0.12.0 + github.com/RobotsAndPencils/buford v0.0.0-00010101000000-000000000000 github.com/client9/reopen v0.0.0-20160619053521-4b86f9c0ead5 github.com/davecgh/go-spew v1.1.1 // indirect github.com/fukata/golang-stats-api-handler v0.0.0-20160325105040-ab9f90f16caa github.com/lestrrat/go-server-starter v0.0.0-20151125041704-901cec093d58 + github.com/pkg/errors v0.8.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312 go.uber.org/atomic v1.1.0 go.uber.org/zap v0.0.0-20170224221842-12592ca48efc - golang.org/x/net v0.0.0-20161116075034-4971afdc2f16 + golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 // indirect + golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 ) + +replace github.com/RobotsAndPencils/buford => github.com/flexfrank/buford v0.13.1-0.20190906024551-21672ff2794e diff --git a/go.sum b/go.sum index 604c42b..3e4cb51 100644 --- a/go.sum +++ b/go.sum @@ -6,10 +6,14 @@ github.com/client9/reopen v0.0.0-20160619053521-4b86f9c0ead5 h1:46QA9E5dIKm6lNyg github.com/client9/reopen v0.0.0-20160619053521-4b86f9c0ead5/go.mod h1:caXVCEr+lUtoN1FlsRiOWdfQtdRHIYfcb0ai8qKWtkQ= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/flexfrank/buford v0.13.1-0.20190906024551-21672ff2794e h1:6pcQA9RJaK0CtoW/BB58Je4BT916gyH5hCWSH2s+B7o= +github.com/flexfrank/buford v0.13.1-0.20190906024551-21672ff2794e/go.mod h1:9w6wdgYczkqCGQWRaV0BB7ygAqNgcnRTHX+1w4E3OWc= github.com/fukata/golang-stats-api-handler v0.0.0-20160325105040-ab9f90f16caa h1:YxLexpeQS0Fz/sNa3QQEgLWyVhgNhEB1GQUi+c45nFs= github.com/fukata/golang-stats-api-handler v0.0.0-20160325105040-ab9f90f16caa/go.mod h1:1sIi4/rHq6s/ednWMZqTmRq3765qTUSs/c3xF6lj8J8= github.com/lestrrat/go-server-starter v0.0.0-20151125041704-901cec093d58 h1:9/ngkqJb42WLtYR6EFRnImztkmF+n5EhwKBxVOj2B3o= github.com/lestrrat/go-server-starter v0.0.0-20151125041704-901cec093d58/go.mod h1:3T+o9dIpjId0dpv2Aa7+HivBIW9h9nra0VuN5ARP/ec= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312 h1:UsFdQ3ZmlzS0BqZYGxvYaXvFGUbCmPGy8DM7qWJJiIQ= @@ -18,5 +22,14 @@ go.uber.org/atomic v1.1.0 h1:wm63V2eSi29VDCF8+5+V0ruTF+GgWT9cW/J9mpkPtRo= go.uber.org/atomic v1.1.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/zap v0.0.0-20170224221842-12592ca48efc h1:pt7EHxvttVwmn5rrlkwEFkjs4OYEOvbLrQgfrNlHTAY= go.uber.org/zap v0.0.0-20170224221842-12592ca48efc/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 h1:0hQKqeLdqlt5iIwVOBErRisrHJAN57yOiPRQItI20fU= +golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/net v0.0.0-20161116075034-4971afdc2f16 h1:x2xFZACPoDbV+g+48fDH/4EQTTNPgHTRko7g0JQiZws= golang.org/x/net v0.0.0-20161116075034-4971afdc2f16/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=