Fixes #661: iOS interruption level support (#679)

Co-authored-by: Ever <47759786+everuribe@users.noreply.github.com>
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Hilário Coelho 2022-06-30 14:44:52 +01:00 committed by GitHub
parent 754e50d482
commit cf67458c43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 32 deletions

View File

@ -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+). | | 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 | | 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 | | 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 ### iOS alert payload

View File

@ -101,21 +101,22 @@ type PushNotification struct {
FastAppTarget int `json:"fast_app_target,omitempty"` FastAppTarget int `json:"fast_app_target,omitempty"`
// iOS // iOS
Expiration *int64 `json:"expiration,omitempty"` Expiration *int64 `json:"expiration,omitempty"`
ApnsID string `json:"apns_id,omitempty"` ApnsID string `json:"apns_id,omitempty"`
CollapseID string `json:"collapse_id,omitempty"` CollapseID string `json:"collapse_id,omitempty"`
Topic string `json:"topic,omitempty"` Topic string `json:"topic,omitempty"`
PushType string `json:"push_type,omitempty"` PushType string `json:"push_type,omitempty"`
Badge *int `json:"badge,omitempty"` Badge *int `json:"badge,omitempty"`
Category string `json:"category,omitempty"` Category string `json:"category,omitempty"`
ThreadID string `json:"thread-id,omitempty"` ThreadID string `json:"thread-id,omitempty"`
URLArgs []string `json:"url-args,omitempty"` URLArgs []string `json:"url-args,omitempty"`
Alert Alert `json:"alert,omitempty"` Alert Alert `json:"alert,omitempty"`
Production bool `json:"production,omitempty"` Production bool `json:"production,omitempty"`
Development bool `json:"development,omitempty"` Development bool `json:"development,omitempty"`
SoundName string `json:"name,omitempty"` SoundName string `json:"name,omitempty"`
SoundVolume float32 `json:"volume,omitempty"` SoundVolume float32 `json:"volume,omitempty"`
Apns D `json:"apns,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 // Bytes for queue message

View File

@ -217,72 +217,76 @@ func configureHTTP2ConnHealthCheck(h2Transport *http2.Transport) {
h2Transport.PingTimeout = 1 * time.Second 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 // Alert dictionary
if len(req.Title) > 0 { 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 { if len(req.Message) > 0 && len(req.Title) > 0 {
payload.AlertBody(req.Message) notificationPayload.AlertBody(req.Message)
} }
if len(req.Alert.Title) > 0 { 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. // Apple Watch & Safari display this string as part of the notification interface.
if len(req.Alert.Subtitle) > 0 { if len(req.Alert.Subtitle) > 0 {
payload.AlertSubtitle(req.Alert.Subtitle) notificationPayload.AlertSubtitle(req.Alert.Subtitle)
} }
if len(req.Alert.TitleLocKey) > 0 { if len(req.Alert.TitleLocKey) > 0 {
payload.AlertTitleLocKey(req.Alert.TitleLocKey) notificationPayload.AlertTitleLocKey(req.Alert.TitleLocKey)
} }
if len(req.Alert.LocArgs) > 0 { if len(req.Alert.LocArgs) > 0 {
payload.AlertLocArgs(req.Alert.LocArgs) notificationPayload.AlertLocArgs(req.Alert.LocArgs)
} }
if len(req.Alert.TitleLocArgs) > 0 { if len(req.Alert.TitleLocArgs) > 0 {
payload.AlertTitleLocArgs(req.Alert.TitleLocArgs) notificationPayload.AlertTitleLocArgs(req.Alert.TitleLocArgs)
} }
if len(req.Alert.Body) > 0 { if len(req.Alert.Body) > 0 {
payload.AlertBody(req.Alert.Body) notificationPayload.AlertBody(req.Alert.Body)
} }
if len(req.Alert.LaunchImage) > 0 { if len(req.Alert.LaunchImage) > 0 {
payload.AlertLaunchImage(req.Alert.LaunchImage) notificationPayload.AlertLaunchImage(req.Alert.LaunchImage)
} }
if len(req.Alert.LocKey) > 0 { if len(req.Alert.LocKey) > 0 {
payload.AlertLocKey(req.Alert.LocKey) notificationPayload.AlertLocKey(req.Alert.LocKey)
} }
if len(req.Alert.Action) > 0 { if len(req.Alert.Action) > 0 {
payload.AlertAction(req.Alert.Action) notificationPayload.AlertAction(req.Alert.Action)
} }
if len(req.Alert.ActionLocKey) > 0 { if len(req.Alert.ActionLocKey) > 0 {
payload.AlertActionLocKey(req.Alert.ActionLocKey) notificationPayload.AlertActionLocKey(req.Alert.ActionLocKey)
} }
// General // General
if len(req.Category) > 0 { if len(req.Category) > 0 {
payload.Category(req.Category) notificationPayload.Category(req.Category)
} }
if len(req.Alert.SummaryArg) > 0 { if len(req.Alert.SummaryArg) > 0 {
payload.AlertSummaryArg(req.Alert.SummaryArg) notificationPayload.AlertSummaryArg(req.Alert.SummaryArg)
} }
if req.Alert.SummaryArgCount > 0 { 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. // GetIOSNotification use for define iOS notification.

View File

@ -526,6 +526,7 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
TitleLocArgs: []string{"a", "b"}, TitleLocArgs: []string{"a", "b"},
TitleLocKey: test, TitleLocKey: test,
}, },
InterruptionLevel: test,
} }
notification := GetIOSNotification(req) notification := GetIOSNotification(req)
@ -546,6 +547,7 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
title, _ := jsonparser.GetString(data, "aps", "alert", "title") title, _ := jsonparser.GetString(data, "aps", "alert", "title")
subtitle, _ := jsonparser.GetString(data, "aps", "alert", "subtitle") subtitle, _ := jsonparser.GetString(data, "aps", "alert", "subtitle")
titleLocKey, _ := jsonparser.GetString(data, "aps", "alert", "title-loc-key") titleLocKey, _ := jsonparser.GetString(data, "aps", "alert", "title-loc-key")
interruptionLevel, _ := jsonparser.GetString(data, "aps", "interruption-level")
aps := dat["aps"].(map[string]interface{}) aps := dat["aps"].(map[string]interface{})
alert := aps["alert"].(map[string]interface{}) alert := aps["alert"].(map[string]interface{})
titleLocArgs := alert["title-loc-args"].([]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, title)
assert.Equal(t, test, subtitle) assert.Equal(t, test, subtitle)
assert.Equal(t, test, titleLocKey) assert.Equal(t, test, titleLocKey)
assert.Equal(t, test, interruptionLevel)
assert.Contains(t, titleLocArgs, "a") assert.Contains(t, titleLocArgs, "a")
assert.Contains(t, titleLocArgs, "b") assert.Contains(t, titleLocArgs, "b")
assert.Contains(t, locArgs, "a") assert.Contains(t, locArgs, "a")