Support Topics Documentation Slack YouTube Blog

Retrieve custom headers in notification service swift

Hi

If I send a push notification template from backendless console, how can I read these custom header values I have sent in the template when the push notification is received in the NotifcationService extension class? If a header is set to a certain value I want to change the content of a push notification.

class NotificationService: UNNotificationServiceExtension {

     override func didReceive() {

    //read value set in the backendless console for compose push notification template header here

   }

}

Regards

Mike

Hello @mike-turner,

Your notification payload

  • must include the mutable-content key with a value of 1
  • must include an alert dictionary with title, subtitle, or body information

If you’re using push templates you can check the “Process in Service Extension” box and that push will go to the Notification Service Extension.

You can find more information about Modifying Content in this documentation.

Regards,
Olha

Hi Olha

Thanks. I did actually read those docs this morning, but still can’t quite grasp it.

If I set a header in the push template like this

myCustomHeader - with a value of myValue

I think I should be able to get this value in the didReceive() method of the extension with
let value = bestAttemptContent.userInfo[“myCustomHeader”]

but this returns nil

However I am not setting a mutable-content key - where do I set that? Is that another header? When I get the info in the service extension the mutable-content looks like it is set ok anyway?

If I get a dump of the bestAttemptContent.userInfo I get this
▿ 3 key/value pairs
▿ (2 elements)
- key: “ios_immediate_push”
- value: {"___jsonclass":“com.backendless.messaging.IOSPushTemplate”,“threadId”:null,“badge”:0,“summaryFormat”:null,“attachmentUrl”:null,“alertSubtitle”:“Discounted golf”,“sound”:“golfball.aiff”,“name”:“TESTNEWSITEMPUSH”,“mutableContent”:1,“actions”:null,“contentAvailable”:0,“customHeaders”:{“myCustomHeader”:“myValue”,“myCustomHeader2”:“31/07/2020 17:36”,“myCustomHeader3”:“31072020”},“alertTitle”:“APP FLASH SALE”} #0
- super: NSMutableString
- super: NSString
- super: NSObject
▿ (2 elements)
- key: “aps”
▿ value: 3 key/value pairs #1
▿ (2 elements)
- key: alert #2
- super: NSString
- super: NSObject
▿ value: 3 key/value pairs #3
▿ (2 elements)
- key: subtitle #4
- super: NSString
- super: NSObject
- value: Discounted golf #5
- super: NSMutableString
- super: NSString
- super: NSObject
▿ (2 elements)
- key: title #6
- super: NSString
- super: NSObject
- value: APP FLASH SALE #7
- super: NSMutableString
- super: NSString
- super: NSObject
▿ (2 elements)
- key: body #8
- super: NSString
- super: NSObject
- value: test #9
- super: NSString
- super: NSObject
▿ (2 elements)
- key: sound #10
- super: NSString
- super: NSObject
- value: golfball.aiff #11
- super: NSMutableString
- super: NSString
- super: NSObject
▿ (2 elements)
- key: mutable-content #12
- super: NSMutableString
- super: NSString
- super: NSObject
- value: 1 #13
- super: NSString
- super: NSObject
▿ (2 elements)
- key: “messageId”
- value: message:7229F46E-1A9F-4023-84E6-81F0462AFE0F #14
- super: NSMutableString
- super: NSString
- super: NSObject

As you can see myCustom headers and values are present, but is there an easy way to get their values without some messy parsing code of bestAttemptContent.userInfo?

Nope, unfortunately the only way is to parse that userInfo.
The structure of push notification is generated on server side and we cannot do anything with it until we get into the NotificationServiceExtension.

Regards,
Olha

OK thanks Olha for clarifying