Skip to content

Commit

Permalink
Fixes #661: iOS interruption level support (#679)
Browse files Browse the repository at this point in the history
Co-authored-by: Ever <[email protected]>
Co-authored-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
3 people authored Jun 30, 2022
1 parent 754e50d commit cf67458
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 16 additions & 15 deletions notify/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 21 additions & 17 deletions notify/notification_apns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions notify/notification_apns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
TitleLocArgs: []string{"a", "b"},
TitleLocKey: test,
},
InterruptionLevel: test,
}

notification := GetIOSNotification(req)
Expand All @@ -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{})
Expand All @@ -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")
Expand Down

0 comments on commit cf67458

Please sign in to comment.