Skip to content

Commit

Permalink
Fix n.Expiration.IsZero and support pushtotalk push notification
Browse files Browse the repository at this point in the history
  • Loading branch information
deltapath-eric committed Oct 15, 2024
1 parent 83ca0a4 commit adbd6e5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func setHeaders(r *http.Request, n *Notification) {
if n.Priority > 0 {
r.Header.Set("apns-priority", strconv.Itoa(n.Priority))
}
if !n.Expiration.IsZero() {
if n.Expiration.After(time.Unix(0, 0)) {
r.Header.Set("apns-expiration", strconv.FormatInt(n.Expiration.Unix(), 10))
}
if n.PushType != "" {
Expand Down
30 changes: 30 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,25 @@ func TestHeaders(t *testing.T) {
assert.NoError(t, err)
}

func TestExpirationHeader(t *testing.T) {
n := mockNotification()
n.ApnsID = "84DB694F-464F-49BD-960A-D6DB028335C9"
n.CollapseID = "game1.start.identifier"
n.Topic = "com.testapp"
n.Priority = 10
n.Expiration = time.Unix(0, 0)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, n.ApnsID, r.Header.Get("apns-id"))
assert.Equal(t, n.CollapseID, r.Header.Get("apns-collapse-id"))
assert.Equal(t, "10", r.Header.Get("apns-priority"))
assert.Equal(t, n.Topic, r.Header.Get("apns-topic"))
assert.Equal(t, "", r.Header.Get("apns-expiration"))
}))
defer server.Close()
_, err := mockClient(server.URL).Push(n)
assert.NoError(t, err)
}

func TestPushTypeAlertHeader(t *testing.T) {
n := mockNotification()
n.PushType = apns.PushTypeAlert
Expand Down Expand Up @@ -333,6 +352,17 @@ func TestPushTypeLiveActivityHeader(t *testing.T) {
assert.NoError(t, err)
}

func TestPushTypePushToTalkHeader(t *testing.T) {
n := mockNotification()
n.PushType = apns.PushTypePushToTalk
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "pushtotalk", r.Header.Get("apns-push-type"))
}))
defer server.Close()
_, err := mockClient(server.URL).Push(n)
assert.NoError(t, err)
}

func TestAuthorizationHeader(t *testing.T) {
n := mockNotification()
token := mockToken()
Expand Down
6 changes: 6 additions & 0 deletions notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ const (
// push type is recommended for iOS. It is not available on macOS, tvOS,
// watchOS and iPadOS.
PushTypeLiveActivity EPushType = "liveactivity"

// PushTypePushToTalk is used for notifications that provide information about the
// push to talk. If you set this push type, the apns-topic header field
// must use your app’s bundle ID with.voip-ptt appended to the end.
// The pushtotalk push type isn’t available on watchOS, macOS, and tvOS. It’s recommended on iOS and iPadOS.
PushTypePushToTalk EPushType = "pushtotalk"
)

const (
Expand Down

0 comments on commit adbd6e5

Please sign in to comment.