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

But 298 add required apns headers #1

Merged
merged 3 commits into from
Sep 30, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ module github.com/vippsas/gozure
require (
golang.org/x/net v0.0.0-20190119204137-ed066c81e75e // indirect
gopkg.in/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc
)
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
golang.org/x/net v0.0.0-20190119204137-ed066c81e75e h1:MDa3fSUp6MdYHouVmCCNz/zaH2a6CRcxY3VhT/K3C5Q=
golang.org/x/net v0.0.0-20190119204137-ed066c81e75e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc h1:LMEBgNcZUqXaP7evD1PZcL6EcDVa2QOFuI+cqM3+AJM=
gopkg.in/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc/go.mod h1:N8UOSI6/c2yOpa/XDz3KVUiegocTziPiqNkeNTMiG1k=
44 changes: 42 additions & 2 deletions notihub/notihub.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
Expand Down Expand Up @@ -152,7 +153,7 @@ func (f NotificationFormat) IsValid() bool {
f == WindowsPhoneFormat
}

// NewNotification initalizes and returns Notification pointer
// NewNotification initializes and returns Notification pointer
func NewNotification(format NotificationFormat, payload []byte) (*Notification, error) {
if !format.IsValid() {
return nil, fmt.Errorf("unknown format '%s'", format)
Expand Down Expand Up @@ -230,7 +231,7 @@ func (h *NotificationHub) SendDirect(ctx context.Context, n *Notification, devic
return b, nil
}

// Schedule pusblishes a scheduled notification to azure notification hub
// Schedule publishes a scheduled notification to azure notification hub
func (h *NotificationHub) Schedule(ctx context.Context, n *Notification, orTags []string, deliverTime time.Time) ([]byte, error) {
b, err := h.send(ctx, n, orTags, &deliverTime)
if err != nil {
Expand All @@ -256,6 +257,17 @@ func (h *NotificationHub) send(ctx context.Context, n *Notification, orTags []st
headers["ServiceBusNotification-Tags"] = strings.Join(orTags, " || ")
}

//IOS 13 and upwards require these headers to be set. They are not set by Notification Hub at the moment, so we need to send them
if n.Format == AppleFormat {
if isAppleBackgroundNotification(n.Payload) {
headers["X-Apns-Push-Type"] = "background"
headers["X-Apns-Priority"] = "5"
} else {
headers["X-Apns-Push-Type"] = "alert"
headers["X-Apns-Priority"] = "10"
}
}

url_ := &url.URL{
Host: h.hubURL.Host,
Scheme: h.hubURL.Scheme,
Expand Down Expand Up @@ -295,6 +307,17 @@ func (h *NotificationHub) sendDirect(ctx context.Context, n *Notification, devic
"X-Apns-Expiration": string(generateExpirationTimestamp()), //apns-expiration
}

//IOS 13 and upwards require these headers to be set. They are not set by Notification Hub at the moment, so we need to send them
if n.Format == AppleFormat {
if isAppleBackgroundNotification(n.Payload) {
headers["X-Apns-Push-Type"] = "background"
headers["X-Apns-Priority"] = "5"
} else {
headers["X-Apns-Push-Type"] = "alert"
headers["X-Apns-Priority"] = "10"
}
}

query := h.hubURL.Query()
query.Add(directParam, "")

Expand Down Expand Up @@ -462,3 +485,20 @@ func (h *NotificationHub) Register(r Registration) (RegistrationRes, []byte, err
}
return regRes, res, err
}

type iosBackgroundNotification struct {
Aps aps `json:"aps"`
}
type aps struct {
ContentAvailable int `json:"content-available"`
}

func isAppleBackgroundNotification(payload []byte) bool {
var backgroundNot iosBackgroundNotification
err := json.Unmarshal(payload, &backgroundNot)
if err != nil {
return false
}

return backgroundNot.Aps.ContentAvailable == 1
}
110 changes: 110 additions & 0 deletions notihub/notihub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package notihub

import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -446,6 +447,115 @@ func Test_NotificationSendError(t *testing.T) {
}
}

func Test_NotificationHubSendAppleBackgroundNotification(t *testing.T) {
n := &iosBackgroundNotification{Aps: aps{ContentAvailable: 1}}
payload, err := json.Marshal(n)
if err != nil {
t.Error(err)
}
var (
errfmt = "Expected %s: %v, got: %v"
notification = &Notification{AppleFormat, payload}

baseURL = &url.URL{
Host: "testHost",
Scheme: schemeDefault,
Path: "testPath",
RawQuery: url.Values{"queryParam": {"queryValue"}}.Encode(),
}
)

mockClient := &mockHubHttpClient{}

nhub := &NotificationHub{
sasKeyName: "testKeyName",
sasKeyValue: "testKeyValue",
hubURL: baseURL,
client: mockClient,
expirationTimeGenerator: expirationTimeGeneratorFunc(func() int64 { return 123 }),
}

msgURL := "https://testHost/testPath/messages?queryParam=queryValue"

mockClient.execFunc = func(obtainedReq *http.Request) ([]byte, error) {
if obtainedReq.Header.Get("X-Apns-Push-Type") != "background" {
t.Errorf(errfmt, "X-Apns-Push-Type", "background", obtainedReq.Header.Get("X-Apns-Push-Type"))
}

if obtainedReq.Header.Get("X-Apns-Priority") != "5" {
t.Errorf(errfmt, "X-Apns-Priority", "5", obtainedReq.Header.Get("X-Apns-Priority"))
}

gotURL := obtainedReq.URL.String()
if gotURL != msgURL {
t.Errorf(errfmt, "URL", msgURL, gotURL)
}

return nil, nil
}

b, err := nhub.Send(context.Background(), notification, nil)
if b != nil {
t.Errorf(errfmt, "byte", nil, b)
}

if err != nil {
t.Errorf(errfmt, "error", nil, err)
}
}

func Test_NotificationHubSendAppleAlertNotification(t *testing.T) {
var (
errfmt = "Expected %s: %v, got: %v"
notification = &Notification{AppleFormat, []byte("{\"aps\":{\"alert\":1}}")}

baseURL = &url.URL{
Host: "testHost",
Scheme: schemeDefault,
Path: "testPath",
RawQuery: url.Values{"queryParam": {"queryValue"}}.Encode(),
}
)

mockClient := &mockHubHttpClient{}

nhub := &NotificationHub{
sasKeyName: "testKeyName",
sasKeyValue: "testKeyValue",
hubURL: baseURL,
client: mockClient,
expirationTimeGenerator: expirationTimeGeneratorFunc(func() int64 { return 123 }),
}

msgURL := "https://testHost/testPath/messages?queryParam=queryValue"

mockClient.execFunc = func(obtainedReq *http.Request) ([]byte, error) {
if obtainedReq.Header.Get("X-Apns-Push-Type") != "alert" {
t.Errorf(errfmt, "X-Apns-Push-Type", "alert", obtainedReq.Header.Get("X-Apns-Push-Type"))
}

if obtainedReq.Header.Get("X-Apns-Priority") != "10" {
t.Errorf(errfmt, "X-Apns-Priority", "10", obtainedReq.Header.Get("X-Apns-Priority"))
}

gotURL := obtainedReq.URL.String()
if gotURL != msgURL {
t.Errorf(errfmt, "URL", msgURL, gotURL)
}

return nil, nil
}

b, err := nhub.Send(context.Background(), notification, nil)
if b != nil {
t.Errorf(errfmt, "byte", nil, b)
}

if err != nil {
t.Errorf(errfmt, "error", nil, err)
}
}

func Test_NotificationScheduleSuccess(t *testing.T) {
var (
errfmt = "Expected %s: %v, got: %v"
Expand Down