How can I set APNS notification identifiers for remote push notifications created using AWS SNS?
Asked Answered
P

2

8

I'm using the AWS SNS SDK in the backend to dispatch remote notifications to my app, and I need to be able to set the notification identifier so that certain notifications replace previously delivered copies by default as described in the iOS developer docs:

If you use the same identifier when scheduling a new notification, the system removes the previously scheduled notification with that identifier and replaces it with the new one.

This is easy to do with local notifications, because they're scheduled in the app where you can set the notification identifier when scheduling the notification.

However, with remote notifications, I can't seem to find any way to set the notification identifier using the SNS SDK (in a Lambda written in .NET).

According to the API reference, SNS returns a response with a message ID (which is likely the notification identifier), but I can't see any way to set the identifier myself.

Does anyone know how to do so? Or does SNS simply not allow this?

Pera answered 25/8, 2018 at 22:5 Comment(0)
P
0

It appears that AWS SNS simply doesn't allow the customization of notification identifiers altogether, nor does it allow the customization of HTTP headers sent to APNS.

The only viable solution for making remote push notifications replace themselves on arrival appears to be by using the Notification Service Extension to check delivered notifications whenever a new one arrives and replace any previous copies with the new version.

It appears to work consistently, and you have the option of either setting a custom payload property or the thread-id property in the notification JSON in order to then check it on-device before delivering each notification.

Pera answered 4/9, 2018 at 20:43 Comment(0)
N
1

You can customize payload, as described by AWS: https://docs.aws.amazon.com/sns/latest/dg/mobile-push-send-custommessage.html

When sending platform-specific payloads in messages using the Amazon SNS console, the data must be key-value pair strings and formatted as JSON with quotation marks escaped. The following example, including formatting and spaces for readability, shows a sample custom message for the GCM platform with key-value pair within the message body and formatted as JSON.

{ "GCM":"{ "data":{ "message":"Check out these awesome deals!", "url":"www.amazon.com" } }" }

You could add your id as a custom field.

EDIT: In case you need to add headers, you may want to proxy the request through a Lambda function: https://docs.aws.amazon.com/sns/latest/dg/sns-lambda.html And this is a thread here showing how to add response headers using Lambda: Setting http response header from AWS lambda

Nonsectarian answered 31/8, 2018 at 21:57 Comment(2)
The problem with that is that, as per the Apple docs page linked there, APNS doesn't accept identifier IDs in the JSON contents of a notification. The only way APNS seems to accept identifier IDs is via the actual POST request to the APN service, but AWS SNS doesn't appear to provide any way of altering HTTP headers sent to APNS -- unless I'm mistaken, which is why I'm asking the above question.Pera
I see. Have you tried to proxy the request through a Lambda function? docs.aws.amazon.com/sns/latest/dg/sns-lambda.htmlNonsectarian
P
0

It appears that AWS SNS simply doesn't allow the customization of notification identifiers altogether, nor does it allow the customization of HTTP headers sent to APNS.

The only viable solution for making remote push notifications replace themselves on arrival appears to be by using the Notification Service Extension to check delivered notifications whenever a new one arrives and replace any previous copies with the new version.

It appears to work consistently, and you have the option of either setting a custom payload property or the thread-id property in the notification JSON in order to then check it on-device before delivering each notification.

Pera answered 4/9, 2018 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.