From 375eec1d9062e5a28123fc92afd7c2561a91b0f1 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 20 Nov 2018 11:02:26 +0800 Subject: [PATCH] feat(ios12): Add support for Summary arguments in notifications (#384) * feat(ios12): Add support for Summary arguments in notifications * feat: upgrade go module --- go.mod | 2 +- go.sum | 2 + gorush/notification.go | 22 +++++---- gorush/notification_apns.go | 9 +++- gorush/notification_apns_test.go | 32 +++++++++++++ .../sideshow/apns2/payload/builder.go | 46 ++++++++++++++----- vendor/vendor.json | 18 ++++---- 7 files changed, 99 insertions(+), 32 deletions(-) diff --git a/go.mod b/go.mod index 19bc55c..fc36260 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,7 @@ require ( github.com/prometheus/client_model v0.0.0-20170216185247-6f3806018612 // indirect github.com/prometheus/common v0.0.0-20170218233558-3007b6072c17 // indirect github.com/prometheus/procfs v0.0.0-20170216223256-a1dba9ce8bae // indirect - github.com/sideshow/apns2 v0.0.0-20180827055107-3c5d4af1700e + github.com/sideshow/apns2 v0.0.0-20181014012405-060d44b53d05 github.com/sirupsen/logrus v0.0.0-20170620144510-3d4380f53a34 github.com/spf13/afero v0.0.0-20171021110813-5660eeed305f // indirect github.com/spf13/cast v1.1.0 // indirect diff --git a/go.sum b/go.sum index 3509d2a..7db64e8 100644 --- a/go.sum +++ b/go.sum @@ -74,6 +74,8 @@ github.com/prometheus/procfs v0.0.0-20170216223256-a1dba9ce8bae h1:4W6eGKy6DVCKy github.com/prometheus/procfs v0.0.0-20170216223256-a1dba9ce8bae/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/sideshow/apns2 v0.0.0-20180827055107-3c5d4af1700e h1:L+MeVX9/PAI/RJkx5YSlwgYtIMNIF6uLb4SVoZsXgLU= github.com/sideshow/apns2 v0.0.0-20180827055107-3c5d4af1700e/go.mod h1:f7dArLPLbiZ3qPdzzrZXdCSlMp8FD0p6z7tHssDOLvk= +github.com/sideshow/apns2 v0.0.0-20181014012405-060d44b53d05 h1:3M+RJyiSlS8LumIYw9HbwwhKMQzJZ1HzTbU3FVmXy8o= +github.com/sideshow/apns2 v0.0.0-20181014012405-060d44b53d05/go.mod h1:f7dArLPLbiZ3qPdzzrZXdCSlMp8FD0p6z7tHssDOLvk= github.com/sirupsen/logrus v0.0.0-20170620144510-3d4380f53a34 h1:kVLTAexkb0RpvzqHGdmxz80/bPLGuZn4qnUR0a4sW9Y= github.com/sirupsen/logrus v0.0.0-20170620144510-3d4380f53a34/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= github.com/spf13/afero v0.0.0-20171021110813-5660eeed305f h1:+Dx5AA/mr18sj78olfUUNWiBBH18xbGhdXiOnLoKnzY= diff --git a/gorush/notification.go b/gorush/notification.go index 514f862..b0762bf 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -30,16 +30,18 @@ const ( // Alert is APNs payload type Alert struct { - Action string `json:"action,omitempty"` - ActionLocKey string `json:"action-loc-key,omitempty"` - Body string `json:"body,omitempty"` - LaunchImage string `json:"launch-image,omitempty"` - LocArgs []string `json:"loc-args,omitempty"` - LocKey string `json:"loc-key,omitempty"` - Title string `json:"title,omitempty"` - Subtitle string `json:"subtitle,omitempty"` - TitleLocArgs []string `json:"title-loc-args,omitempty"` - TitleLocKey string `json:"title-loc-key,omitempty"` + Action string `json:"action,omitempty"` + ActionLocKey string `json:"action-loc-key,omitempty"` + Body string `json:"body,omitempty"` + LaunchImage string `json:"launch-image,omitempty"` + LocArgs []string `json:"loc-args,omitempty"` + LocKey string `json:"loc-key,omitempty"` + Title string `json:"title,omitempty"` + Subtitle string `json:"subtitle,omitempty"` + TitleLocArgs []string `json:"title-loc-args,omitempty"` + TitleLocKey string `json:"title-loc-key,omitempty"` + SummaryArg string `json:"summary-arg,omitempty"` + SummaryArgCount int `json:"summary-arg-count,omitempty"` } // RequestPush support multiple notification request. diff --git a/gorush/notification_apns.go b/gorush/notification_apns.go index a07b185..31c93d9 100644 --- a/gorush/notification_apns.go +++ b/gorush/notification_apns.go @@ -148,11 +148,18 @@ func iosAlertDictionary(payload *payload.Payload, req PushNotification) *payload } // General - if len(req.Category) > 0 { payload.Category(req.Category) } + if len(req.Alert.SummaryArg) > 0 { + payload.AlertSummaryArg(req.Alert.SummaryArg) + } + + if req.Alert.SummaryArgCount > 0 { + payload.AlertSummaryArgCount(req.Alert.SummaryArgCount) + } + return payload } diff --git a/gorush/notification_apns_test.go b/gorush/notification_apns_test.go index e6ed577..f780f0d 100644 --- a/gorush/notification_apns_test.go +++ b/gorush/notification_apns_test.go @@ -157,6 +157,38 @@ func TestIOSSoundAndVolume(t *testing.T) { assert.Equal(t, int64(1), soundCritical) } +func TestIOSSummaryArg(t *testing.T) { + var dat map[string]interface{} + + test := "test" + message := "Welcome notification Server" + req := PushNotification{ + ApnsID: test, + Topic: test, + Priority: "normal", + Message: message, + Alert: Alert{ + SummaryArg: "test", + SummaryArgCount: 3, + }, + } + + notification := GetIOSNotification(req) + + dump, _ := json.Marshal(notification.Payload) + data := []byte(string(dump)) + + if err := json.Unmarshal(data, &dat); err != nil { + panic(err) + } + + assert.Equal(t, test, notification.ApnsID) + assert.Equal(t, test, notification.Topic) + assert.Equal(t, ApnsPriorityLow, notification.Priority) + assert.Equal(t, "test", dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["summary-arg"]) + assert.Equal(t, float64(3), dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["summary-arg-count"]) +} + // Silent Notification which payload’s aps dictionary must not contain the alert, sound, or badge keys. // ref: https://goo.gl/m9xyqG func TestSendZeroValueForBadgeKey(t *testing.T) { diff --git a/vendor/github.com/sideshow/apns2/payload/builder.go b/vendor/github.com/sideshow/apns2/payload/builder.go index 6e96472..6f83a8e 100644 --- a/vendor/github.com/sideshow/apns2/payload/builder.go +++ b/vendor/github.com/sideshow/apns2/payload/builder.go @@ -22,16 +22,18 @@ type aps struct { } type alert struct { - Action string `json:"action,omitempty"` - ActionLocKey string `json:"action-loc-key,omitempty"` - Body string `json:"body,omitempty"` - LaunchImage string `json:"launch-image,omitempty"` - LocArgs []string `json:"loc-args,omitempty"` - LocKey string `json:"loc-key,omitempty"` - Title string `json:"title,omitempty"` - Subtitle string `json:"subtitle,omitempty"` - TitleLocArgs []string `json:"title-loc-args,omitempty"` - TitleLocKey string `json:"title-loc-key,omitempty"` + Action string `json:"action,omitempty"` + ActionLocKey string `json:"action-loc-key,omitempty"` + Body string `json:"body,omitempty"` + LaunchImage string `json:"launch-image,omitempty"` + LocArgs []string `json:"loc-args,omitempty"` + LocKey string `json:"loc-key,omitempty"` + Title string `json:"title,omitempty"` + Subtitle string `json:"subtitle,omitempty"` + TitleLocArgs []string `json:"title-loc-args,omitempty"` + TitleLocKey string `json:"title-loc-key,omitempty"` + SummaryArg string `json:"summary-arg,omitempty"` + SummaryArgCount int `json:"summary-arg-count,omitempty"` } type sound struct { @@ -235,13 +237,35 @@ func (p *Payload) AlertActionLocKey(key string) *Payload { return p } +// AlertSummaryArg sets the aps alert summary arg key on the payload. +// This is the string that is used as a key to fill in an argument +// at the bottom of a notification to provide more context, such as +// a name associated with the sender of the notification. +// +// {"aps":{"alert":{"summary-arg":key}}} +func (p *Payload) AlertSummaryArg(key string) *Payload { + p.aps().alert().SummaryArg = key + return p +} + +// AlertSummaryArgCount sets the aps alert summary arg count key on the payload. +// This integer sets a custom "weight" on the notification, effectively +// allowing a notification to be viewed internally as two. For example if +// a notification encompasses 3 messages, you can set it to 3. +// +// {"aps":{"alert":{"summary-arg-count":key}}} +func (p *Payload) AlertSummaryArgCount(key int) *Payload { + p.aps().alert().SummaryArgCount = key + return p +} + // General // Category sets the aps category on the payload. // This is a string value that represents the identifier property of the // UIMutableUserNotificationCategory object you created to define custom actions. // -// {"aps":{"alert":{"category":category}}} +// {"aps":{"category":category}} func (p *Payload) Category(category string) *Payload { p.aps().Category = category return p diff --git a/vendor/vendor.json b/vendor/vendor.json index 1078ad6..ccabdfc 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -449,22 +449,22 @@ { "checksumSHA1": "DL5vX9aOefzIAxMDdaQBtgRdR0Y=", "path": "github.com/sideshow/apns2", - "revision": "3c5d4af1700ed9111ecb16a9a99a92a00c8b2f27", - "revisionTime": "2018-08-27T05:51:07Z", - "version": "master", - "versionExact": "master" + "revision": "5c6f7afc0242dbeab1c0b26a1efd761a67228004", + "revisionTime": "2018-10-12T08:22:23Z", + "version": "v0.15", + "versionExact": "v0.15" }, { "checksumSHA1": "EHOwwdWPJGt1vNVeJxyRDRHBhl8=", "path": "github.com/sideshow/apns2/certificate", - "revision": "c6554aff77e6e5580dec977c8c33cc238f329ab0", - "revisionTime": "2018-04-13T21:53:35Z" + "revision": "060d44b53d05502e715958efde28063cd0746dba", + "revisionTime": "2018-10-14T01:24:05Z" }, { - "checksumSHA1": "36bPwB1aiQCxfmdwfgYmI89I9nI=", + "checksumSHA1": "uwySZ0Z4LZ+9PIYqg7twpCQxv+I=", "path": "github.com/sideshow/apns2/payload", - "revision": "3c5d4af1700ed9111ecb16a9a99a92a00c8b2f27", - "revisionTime": "2018-08-27T05:51:07Z" + "revision": "060d44b53d05502e715958efde28063cd0746dba", + "revisionTime": "2018-10-14T01:24:05Z" }, { "checksumSHA1": "nkQ/1JoIY4jh8XlI8LClfFVux9U=",