From adbd6e57c779432455d5397effc51daa09756f68 Mon Sep 17 00:00:00 2001 From: deltapath-eric Date: Tue, 15 Oct 2024 15:33:45 +0800 Subject: [PATCH] Fix n.Expiration.IsZero and support pushtotalk push notification --- client.go | 2 +- client_test.go | 30 ++++++++++++++++++++++++++++++ notification.go | 6 ++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index d36da1b..832b421 100644 --- a/client.go +++ b/client.go @@ -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 != "" { diff --git a/client_test.go b/client_test.go index cfbdb6c..60fd058 100644 --- a/client_test.go +++ b/client_test.go @@ -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 @@ -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() diff --git a/notification.go b/notification.go index 5cbc921..54c8caf 100644 --- a/notification.go +++ b/notification.go @@ -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 (