From 36bc4f0f4b09113de93b212b9c35c0da165f088f Mon Sep 17 00:00:00 2001 From: Alexander Widerberg Date: Wed, 16 Dec 2020 23:48:00 +0100 Subject: [PATCH] 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 --- gorush/notification_apns.go | 8 +++-- gorush/notification_apns_test.go | 56 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/gorush/notification_apns.go b/gorush/notification_apns.go index bb27457..13aa71d 100644 --- a/gorush/notification_apns.go +++ b/gorush/notification_apns.go @@ -201,6 +201,10 @@ func iosAlertDictionary(payload *payload.Payload, req PushNotification) *payload payload.AlertTitle(req.Title) } + if len(req.Message) > 0 && len(req.Title) > 0 { + payload.AlertBody(req.Message) + } + if len(req.Alert.Title) > 0 { payload.AlertTitle(req.Alert.Title) } @@ -286,8 +290,8 @@ func GetIOSNotification(req PushNotification) *apns2.Notification { payload := payload.NewPayload() - // add alert object if message length > 0 - if len(req.Message) > 0 { + // add alert object if message length > 0 and title length == 0 + if len(req.Message) > 0 && len(req.Title) == 0 { payload.Alert(req.Message) } diff --git a/gorush/notification_apns_test.go b/gorush/notification_apns_test.go index a24b4f1..91ebb8d 100644 --- a/gorush/notification_apns_test.go +++ b/gorush/notification_apns_test.go @@ -450,6 +450,62 @@ func TestAlertStringExample3ForIos(t *testing.T) { 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) { var dat map[string]interface{}