From cf67458c43c55610eb6a5674fb3991224ec44fc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hil=C3=A1rio=20Coelho?= Date: Thu, 30 Jun 2022 14:44:52 +0100 Subject: [PATCH] Fixes #661: iOS interruption level support (#679) Co-authored-by: Ever <47759786+everuribe@users.noreply.github.com> Co-authored-by: Bo-Yi Wu --- README.md | 1 + notify/notification.go | 31 +++++++++++++------------- notify/notification_apns.go | 38 ++++++++++++++++++-------------- notify/notification_apns_test.go | 3 +++ 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 749b94e..4f0dde4 100644 --- a/README.md +++ b/README.md @@ -655,6 +655,7 @@ The Request body must have a notifications array. The following is a parameter t | mutable_content | bool | enable Notification Service app extension. | - | only iOS(10.0+). | | name | string | sets the name value on the aps sound dictionary. | - | only iOS | | volume | float32 | sets the volume value on the aps sound dictionary. | - | only iOS | +| interruption_level | string | defines the interruption level for the push notification. | - | only iOS(15.0+) | ### iOS alert payload diff --git a/notify/notification.go b/notify/notification.go index 9a724fa..983144f 100644 --- a/notify/notification.go +++ b/notify/notification.go @@ -101,21 +101,22 @@ type PushNotification struct { FastAppTarget int `json:"fast_app_target,omitempty"` // iOS - Expiration *int64 `json:"expiration,omitempty"` - ApnsID string `json:"apns_id,omitempty"` - CollapseID string `json:"collapse_id,omitempty"` - Topic string `json:"topic,omitempty"` - PushType string `json:"push_type,omitempty"` - Badge *int `json:"badge,omitempty"` - Category string `json:"category,omitempty"` - ThreadID string `json:"thread-id,omitempty"` - URLArgs []string `json:"url-args,omitempty"` - Alert Alert `json:"alert,omitempty"` - Production bool `json:"production,omitempty"` - Development bool `json:"development,omitempty"` - SoundName string `json:"name,omitempty"` - SoundVolume float32 `json:"volume,omitempty"` - Apns D `json:"apns,omitempty"` + Expiration *int64 `json:"expiration,omitempty"` + ApnsID string `json:"apns_id,omitempty"` + CollapseID string `json:"collapse_id,omitempty"` + Topic string `json:"topic,omitempty"` + PushType string `json:"push_type,omitempty"` + Badge *int `json:"badge,omitempty"` + Category string `json:"category,omitempty"` + ThreadID string `json:"thread-id,omitempty"` + URLArgs []string `json:"url-args,omitempty"` + Alert Alert `json:"alert,omitempty"` + Production bool `json:"production,omitempty"` + Development bool `json:"development,omitempty"` + SoundName string `json:"name,omitempty"` + SoundVolume float32 `json:"volume,omitempty"` + Apns D `json:"apns,omitempty"` + InterruptionLevel string `json:"interruption_level,omitempty"` // ref: https://github.com/sideshow/apns2/blob/54928d6193dfe300b6b88dad72b7e2ae138d4f0a/payload/builder.go#L7-L24 } // Bytes for queue message diff --git a/notify/notification_apns.go b/notify/notification_apns.go index d1b2a9d..1ddb9b6 100644 --- a/notify/notification_apns.go +++ b/notify/notification_apns.go @@ -217,72 +217,76 @@ func configureHTTP2ConnHealthCheck(h2Transport *http2.Transport) { h2Transport.PingTimeout = 1 * time.Second } -func iosAlertDictionary(payload *payload.Payload, req *PushNotification) *payload.Payload { +func iosAlertDictionary(notificationPayload *payload.Payload, req *PushNotification) *payload.Payload { // Alert dictionary if len(req.Title) > 0 { - payload.AlertTitle(req.Title) + notificationPayload.AlertTitle(req.Title) + } + + if len(req.InterruptionLevel) > 0 { + notificationPayload.InterruptionLevel(payload.EInterruptionLevel(req.InterruptionLevel)) } if len(req.Message) > 0 && len(req.Title) > 0 { - payload.AlertBody(req.Message) + notificationPayload.AlertBody(req.Message) } if len(req.Alert.Title) > 0 { - payload.AlertTitle(req.Alert.Title) + notificationPayload.AlertTitle(req.Alert.Title) } // Apple Watch & Safari display this string as part of the notification interface. if len(req.Alert.Subtitle) > 0 { - payload.AlertSubtitle(req.Alert.Subtitle) + notificationPayload.AlertSubtitle(req.Alert.Subtitle) } if len(req.Alert.TitleLocKey) > 0 { - payload.AlertTitleLocKey(req.Alert.TitleLocKey) + notificationPayload.AlertTitleLocKey(req.Alert.TitleLocKey) } if len(req.Alert.LocArgs) > 0 { - payload.AlertLocArgs(req.Alert.LocArgs) + notificationPayload.AlertLocArgs(req.Alert.LocArgs) } if len(req.Alert.TitleLocArgs) > 0 { - payload.AlertTitleLocArgs(req.Alert.TitleLocArgs) + notificationPayload.AlertTitleLocArgs(req.Alert.TitleLocArgs) } if len(req.Alert.Body) > 0 { - payload.AlertBody(req.Alert.Body) + notificationPayload.AlertBody(req.Alert.Body) } if len(req.Alert.LaunchImage) > 0 { - payload.AlertLaunchImage(req.Alert.LaunchImage) + notificationPayload.AlertLaunchImage(req.Alert.LaunchImage) } if len(req.Alert.LocKey) > 0 { - payload.AlertLocKey(req.Alert.LocKey) + notificationPayload.AlertLocKey(req.Alert.LocKey) } if len(req.Alert.Action) > 0 { - payload.AlertAction(req.Alert.Action) + notificationPayload.AlertAction(req.Alert.Action) } if len(req.Alert.ActionLocKey) > 0 { - payload.AlertActionLocKey(req.Alert.ActionLocKey) + notificationPayload.AlertActionLocKey(req.Alert.ActionLocKey) } // General if len(req.Category) > 0 { - payload.Category(req.Category) + notificationPayload.Category(req.Category) } if len(req.Alert.SummaryArg) > 0 { - payload.AlertSummaryArg(req.Alert.SummaryArg) + notificationPayload.AlertSummaryArg(req.Alert.SummaryArg) } if req.Alert.SummaryArgCount > 0 { - payload.AlertSummaryArgCount(req.Alert.SummaryArgCount) + notificationPayload.AlertSummaryArgCount(req.Alert.SummaryArgCount) } - return payload + return notificationPayload } // GetIOSNotification use for define iOS notification. diff --git a/notify/notification_apns_test.go b/notify/notification_apns_test.go index 6a6c97c..e1425c6 100644 --- a/notify/notification_apns_test.go +++ b/notify/notification_apns_test.go @@ -526,6 +526,7 @@ func TestIOSAlertNotificationStructure(t *testing.T) { TitleLocArgs: []string{"a", "b"}, TitleLocKey: test, }, + InterruptionLevel: test, } notification := GetIOSNotification(req) @@ -546,6 +547,7 @@ func TestIOSAlertNotificationStructure(t *testing.T) { title, _ := jsonparser.GetString(data, "aps", "alert", "title") subtitle, _ := jsonparser.GetString(data, "aps", "alert", "subtitle") titleLocKey, _ := jsonparser.GetString(data, "aps", "alert", "title-loc-key") + interruptionLevel, _ := jsonparser.GetString(data, "aps", "interruption-level") aps := dat["aps"].(map[string]interface{}) alert := aps["alert"].(map[string]interface{}) titleLocArgs := alert["title-loc-args"].([]interface{}) @@ -559,6 +561,7 @@ func TestIOSAlertNotificationStructure(t *testing.T) { assert.Equal(t, test, title) assert.Equal(t, test, subtitle) assert.Equal(t, test, titleLocKey) + assert.Equal(t, test, interruptionLevel) assert.Contains(t, titleLocArgs, "a") assert.Contains(t, titleLocArgs, "b") assert.Contains(t, locArgs, "a")