Why Is Building iOS Notifications So Annoying?
04 Feb 2022At the risk of this blog turning into a litany of engineering complaints, I’ve decided to write about our experience with building iOS notifications into our mobile application. We were using Expo and React Native with AWS’s Amplify.
When notifications are and are not part of your 1.0
Notifications likely should be part of the first version of your app on the App store. For social media applications, users expect to be notified when a friend messages or likes something of theirs in-app. Additionally, social media traffic is heavily reliant on notifications bringing users back into the application. Enterprise mobile applications don’t suffer from this nearly as much – usually, people need to log into enterprise mobile applications to conduct business. However, enterprise users will likely need to be notified of important status changes in their business.
Thus, most applications need at least a few, basic notifications to be truly viable for GA, and most mobile apps don’t yet need to spring for an expensive notification-monitoring tool. So, what’s the best system for notifications when using an Amplify backend?
Given use case
I wanted to fulfill the following use case: user A likes user B’s post. User B, if they have enrolled in notifications, is notified that user A liked their post.
So, this is what I needed to do to make notifications work:
- Take and store a push notification token alongside a person’s userID in my database
- Listen to the database changes and kick off a function when a change happened
- Make a function that sends a request to Apple’s Push notification API
AWS Amplify’s complicated “Notifications”
Amplify pushes Amazon’s Pinpoint product. I couldn’t figure out how to make the “segments” fit my use case of individualized transactional push notifications in a simple way: to use the Pinpoint product, you need to set up actions.
I thought that there had to be a way to use the existing AWS backend to send my initial set of notifications.
Avoiding Expo’s notifications
Any solution that suggests using Expo’s notification service is met with an ominous warning from Expo itself: “Currently using Expo’s notification service is free but we expect to make it part of our paid services.” Additionally, it seems to make no sense to use Expo’s notification service when Apple’s does the exact same thing (you just swap Expo push notification tokens for Apple push notification tokens).
Using Amplify for uncomplicated notifications
The trickiest part of all this was that I was using Amplify for my backend, and I needed to be able to control the “serverless” server and listen for changes to my database.
It turns out this is actually really easy. AWS Lambda — which is the AWS product that allows you to build backend functions — has built-in functionality to listen to your databases.
This is how I set my function up:
In this case, my function is listening to my Post table.
Now we get to the fun part: calling Apple’s push notification API within this Lambda function. Using “jsonwebtoken”, “http2”, and “fs” makes this fairly straightforward, as long as you make sure your Lambda is waiting long enough to listen to Apple’s API response. Example gist below.
Make sure you add the required node modules to the folder that contains your Lambda function in your project folder. Once you add in the capabiltiy for requesting permissions and Apple Push Tokens within your app and save them into your database, you can easily use them in your Lambda function as well.
Then you’re done! Enjoy your push notifications without enrolling in more tools or products.