From a0adba5e0e50dd1655b2f7bf5cd8ca0433901893 Mon Sep 17 00:00:00 2001 From: Julia Rzhevskaia Date: Thu, 8 Aug 2024 10:59:37 +0200 Subject: [PATCH] add subtitle-loc-key and subtitle-loc-args in payload alert (#218) --- payload/builder.go | 24 ++++++++++++++++++++++++ payload/builder_test.go | 12 ++++++++++++ 2 files changed, 36 insertions(+) diff --git a/payload/builder.go b/payload/builder.go index 7d0cd5a..dbda145 100644 --- a/payload/builder.go +++ b/payload/builder.go @@ -71,6 +71,8 @@ type alert struct { Subtitle string `json:"subtitle,omitempty"` TitleLocArgs []string `json:"title-loc-args,omitempty"` TitleLocKey string `json:"title-loc-key,omitempty"` + SubtitleLocArgs []string `json:"subtitle-loc-args,omitempty"` + SubtitleLocKey string `json:"subtitle-loc-key,omitempty"` SummaryArg string `json:"summary-arg,omitempty"` SummaryArgCount int `json:"summary-arg-count,omitempty"` } @@ -274,6 +276,28 @@ func (p *Payload) AlertSubtitle(subtitle string) *Payload { return p } +// AlertSubtitleLocKey sets the aps alert subtitle localization key on the payload. +// This is the key to a subtitle string in the Localizable.strings file for the +// current localization. See Localized Formatted Strings in Apple documentation +// for more information. +// +// {"aps":{"alert":{"subtitle-loc-key":key}}} +func (p *Payload) AlertSubtitleLocKey(key string) *Payload { + p.aps().alert().SubtitleLocKey = key + return p +} + +// AlertSubtitleLocArgs sets the aps alert subtitle localization args on the payload. +// These are the variable string values to appear in place of the format +// specifiers in subtitle-loc-key. See Localized Formatted Strings in Apple +// documentation for more information. +// +// {"aps":{"alert":{"title-loc-args":args}}} +func (p *Payload) AlertSubtitleLocArgs(args []string) *Payload { + p.aps().alert().SubtitleLocArgs = args + return p +} + // AlertBody sets the aps alert body on the payload. // This is the text of the alert message. // diff --git a/payload/builder_test.go b/payload/builder_test.go index b0351d0..cfde381 100644 --- a/payload/builder_test.go +++ b/payload/builder_test.go @@ -111,6 +111,18 @@ func TestAlertSubtitle(t *testing.T) { assert.Equal(t, `{"aps":{"alert":{"subtitle":"hello"}}}`, string(b)) } +func TestAlertSubtitleLocKey(t *testing.T) { + payload := NewPayload().AlertSubtitleLocKey("Notification.Key.TestSubtitle") + b, _ := json.Marshal(payload) + assert.Equal(t, `{"aps":{"alert":{"subtitle-loc-key":"Notification.Key.TestSubtitle"}}}`, string(b)) +} + +func TestAlertSubtitleLocArgs(t *testing.T) { + payload := NewPayload().AlertSubtitleLocArgs([]string{"one", "two"}) + b, _ := json.Marshal(payload) + assert.Equal(t, `{"aps":{"alert":{"subtitle-loc-args":["one","two"]}}}`, string(b)) +} + func TestAlertBody(t *testing.T) { payload := NewPayload().AlertBody("body") b, _ := json.Marshal(payload)