Sunrise iOS app push notifications are triggered using Commercetools subscriptions. One of the possible destinations for a subscription is an IronMQ. Messages published to an IronMQ can trigger IronWorker 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. - Docker Hub account for pushing the Docker image built from the
NotificationUtil.groovy
script. - Active Iron.io account.
- Before building the JAR file containing the script and all other dependencies, you need to copy your
.p12
APNS certificate tosrc/main/resources
directory. Make sure to replaceInputStream certificate = this.getClass().getResourceAsStream('/Sunrise.p12')
with your certificate name. - 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
- Create a
Dockerfile
in the same directory where your newly created.jar
file is located.
FROM iron/java
WORKDIR /app
ADD . /app
ENTRYPOINT ["java", "-jar", "notification-service.jar"]
-
Make sure to replace
notification-service.jar
with the proper name of the JAR file you have built. -
Build your Docker image:
docker build -t USERNAME/IMAGENAME:0.0.1 .
, where USERNAME and IMAGENAME should match the name of your Docker hub username and the image name you want to assign to your notification service. -
Push it to Docker Hub:
docker push USERNAME/IMAGENAME:0.0.1
. -
Register your image with Iron:
iron register USERNAME/IMAGENAME:0.0.1
.
- Navigate to the MQ section of the Iron.io dashboard, and create a new unicast queue. For the subscriber URL, paste the Webhook URL of the IronWorker you created in the previous step.
- Copy the Webhook URL of your IronMQ, and use it to subscribe to
OrderCreated
message.
- 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 IronMQ you subscribed. The queue triggers the IronWorker, which retrieves customer's token from the Commercetools API, and sends the notification payload to the Apple's production notification server.