Sunrise iOS app push notifications are triggered using Commercetools subscriptions. One of the possible destinations for a subscription is an AWS SNS topic. Messages published to a topic can trigger lambda functions if subscribed.
This example will show you how you can easily get notifications to your mobile shop app running on Commercetools platform, without a need for a server instance. The scope of this example covers reservation notification scenario, triggered on OrderCreated
message.
In order to use this example you need:
- An active Commercetools project. Replace
PROJECT_KEY
,SCOPE
,CLIENT_ID
,CLIENT_SECRET
,API_URL
andAUTH_URL
with values valid for your project. - AWS access key. Replace
AWS_ACCESS_KEY
, andAWS_SECRET_KEY
with your key and secret.
- Navigate to the SNS section of the AWS console.
- Create new topic, copy the
ARN
and use it to subscribe toOrderCreated
message. - Now navigate to Applications section, and create new platform application. For the push notification platform, you should select Apple production or Apple development depending on your current app environment. Either upload the certificate file, or paste the certificate and private key into the popup and confirm to save.
- Copy the
ARN
you got for the newly created application, and set that value inNotificationUtil.groovy
, lineplatformEndpointRequest.setPlatformApplicationArn("arn:aws:sns:us-west-2:487164526243:app/APNS/Sunrise")
.
- Run
gradle fatJar
to build a JAR file which contains all dependencies used in theNotificationUtil.groovy
script.- Optionally, you can use
gradle runScript
command to run the script locally for debugging purposes.
- Optionally, you can use
- Navigate to the Lambda section of the AWS console.
- Create new Lambda function, and use configure function section to set the following values:
- Configure runtime to Java 8;
- Function package should contain the JAR file created in the previous step;
- Handler should be set to de.commercetools.NotificationService::handleRequest if you haven't changed the package name;
- Configure triggers section allow you to specify a trigger which will invoke your Lambda function. You should select SNS from the dropdown menu, and pick the topic created in the second step.
- Login with a valid customer account on your Sunrise app instance, and make sure to allow push notifications. Customer's push token will be stored in a Customer's custom field.
- Pick some product and make a reservation.
- The Commercetools platform sends an
OrderCreated
message to the SNS you subscribed. The topic triggers the Lambda function, which retrieves customer's token from the Commercetools API, and passes it, along with the notification payload to the APNS platform application.