Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

support apns-push-type to the request header for iOS 13+ #122

Merged
merged 1 commit into from
Sep 17, 2019
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
3 changes: 2 additions & 1 deletion SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
13 changes: 12 additions & 1 deletion gaurun/apns_http2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions gaurun/apns_http2_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 5 additions & 0 deletions gaurun/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ const (
StatusFailedPush = "failed-push"
StatusDisabledPush = "disabled-push"
)

const (
ApnsPushTypeAlert = "alert"
ApnsPushTypeBackground = "background"
)
7 changes: 7 additions & 0 deletions gaurun/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}

Expand Down
27 changes: 27 additions & 0 deletions gaurun/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=