iOS: Allow message and title to be used together, without using the alert-payload (#553)

This PR will allow callers to use the Message & Title without the need of setting any values in the alert-payload, essentially making it easier to build cross-device-push for regular title+message pushes on different platforms.

Co-authored-by: Alexander Widerberg <alexander.widerberg@xmreality.se>
This commit is contained in:
Alexander Widerberg 2020-12-16 23:48:00 +01:00 committed by GitHub
parent f32e5e838b
commit 36bc4f0f4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 2 deletions

View File

@ -201,6 +201,10 @@ func iosAlertDictionary(payload *payload.Payload, req PushNotification) *payload
payload.AlertTitle(req.Title) payload.AlertTitle(req.Title)
} }
if len(req.Message) > 0 && len(req.Title) > 0 {
payload.AlertBody(req.Message)
}
if len(req.Alert.Title) > 0 { if len(req.Alert.Title) > 0 {
payload.AlertTitle(req.Alert.Title) payload.AlertTitle(req.Alert.Title)
} }
@ -286,8 +290,8 @@ func GetIOSNotification(req PushNotification) *apns2.Notification {
payload := payload.NewPayload() payload := payload.NewPayload()
// add alert object if message length > 0 // add alert object if message length > 0 and title length == 0
if len(req.Message) > 0 { if len(req.Message) > 0 && len(req.Title) == 0 {
payload.Alert(req.Message) payload.Alert(req.Message)
} }

View File

@ -450,6 +450,62 @@ func TestAlertStringExample3ForIos(t *testing.T) {
assert.Equal(t, test, dat["aps"].(map[string]interface{})["alert"]) assert.Equal(t, test, dat["aps"].(map[string]interface{})["alert"])
} }
func TestMessageAndTitle(t *testing.T) {
var dat map[string]interface{}
test := "test"
message := "Welcome notification Server"
title := "Welcome notification Server title"
req := PushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
Message: message,
Title: title,
ContentAvailable: true,
}
notification := GetIOSNotification(req)
dump, _ := json.Marshal(notification.Payload)
data := []byte(string(dump))
if err := json.Unmarshal(data, &dat); err != nil {
log.Println(err)
panic(err)
}
alert, _ := jsonparser.GetString(data, "aps", "alert")
alertBody, _ := jsonparser.GetString(data, "aps", "alert", "body")
alertTitle, _ := jsonparser.GetString(data, "aps", "alert", "title")
assert.Equal(t, test, notification.ApnsID)
assert.Equal(t, ApnsPriorityLow, notification.Priority)
assert.Equal(t, message, alertBody)
assert.Equal(t, title, alertTitle)
assert.NotEqual(t, message, alert)
// Add alert body
messageOverride := "Welcome notification Server overridden"
req.Alert.Body = messageOverride
notification = GetIOSNotification(req)
dump, _ = json.Marshal(notification.Payload)
data = []byte(string(dump))
if err := json.Unmarshal(data, &dat); err != nil {
log.Println(err)
panic(err)
}
alertBodyOverridden, _ := jsonparser.GetString(data, "aps", "alert", "body")
alertTitle, _ = jsonparser.GetString(data, "aps", "alert", "title")
assert.Equal(t, messageOverride, alertBodyOverridden)
assert.NotEqual(t, message, alertBodyOverridden)
assert.Equal(t, title, alertTitle)
}
func TestIOSAlertNotificationStructure(t *testing.T) { func TestIOSAlertNotificationStructure(t *testing.T) {
var dat map[string]interface{} var dat map[string]interface{}