EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
Atlas
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Atlaschevron-right

Push Notifications Using Atlas App Services & iOS Realm SDK

Josman Pérez Expóstio8 min read • Published Jul 19, 2022 • Updated Jul 19, 2022
Google CloudiOSAtlasSwiftJavaScript
Facebook Icontwitter iconlinkedin icon
Push Notifications Using Atlas App Services & iOS Realm SDK
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
In a serverless application, one of the important features that we must implement for the success of our application is push notifications.
Realm allows us to have a complete push notification system directly in our Services App. To do this, we’ll make use of several components that we’ll explain here and develop in this tutorial. But first, let’s describe what our application does.

Context

The application consists of a public list of books that are stored locally on the device thanks to Atlas Device Sync, so we can add/delete or update the books directly in our Atlas collection and the changes will be reflected in our application.
We can also add as favorites any book from the list, although to do so, it’ll be necessary to register beforehand using an email and password. We will integrate email/password authentication in our application through the Atlas App Services authentication providers.
The books, as they belong to a collection synchronized with Atlas Device Sync, will not only be stored persistently on our device but will also be synchronized with our user. This means that we can retrieve the list of favorites on any device where we register with our credentials. Changes made to our favorites using other devices are automatically synchronized.
Example of the iOS app running on a device

Firebase

The management of push notifications will be done through Firebase Cloud Messaging. In this way, we benefit from a single service to send notifications to iOS, web, and Android.
The configuration is similar to the one we would follow for any other application. The difference is that we will install the firebase-admin SDK in our Atlas App Services application.

Triggers

The logic of this application for sending push notifications will be done through triggers. To do this, we will define two use cases:
  1. A book has been added or deleted: For this, we will make use of the topics in Firebase, so when a user registers to receive this type of notification, they will receive a message every time a book is added/deleted from the general list of books.
  2. A book added to my favorites list has been modified: We will make use of the Firebase tokens for each device. We relate the token received to the user so that when a book is modified, only the user/s that have it in their favorites list will receive the notification.

Functions

The Atlas Triggers will have a function linked that will apply the logic for sending push notifications to the end devices. We will make use of the Firebase Admin SDK that we will install in our App Services App as a dependency.

Overall application logic

This application will show how we can integrate push notifications with an iOS application developed in Swift. We will discuss how we have created each part with code, diagrams, and example usage.
At the end of this tutorial, you’ll find a link to a Github repository where you’ll find both the code of the iOS application as well as the code of the App Services application.
How to request a FCMToken
When we start the application for the first time, we log in using anonymous authentication, since to view the list of books, it’s not necessary to register using email/password. However, an anonymous user will still be created and saved in a collection Users in Atlas.
When we first access our application and enable push notifications, in our code, we register with Firebase. This will generate a registration token, also known as FCMToken, that we will use later to send custom push notifications.
Once we obtain the FCM token, we will call a function through the Realm SDK to save this token in the user document corresponding to the logged-in user. Within the document, we have defined a token field that will be composed of an array of FCM tokens.
To do this, we will make use of the Firebase SDK and the Messaging method, so that we are notified every time the token changes or a new token is generated. In our Swift code, we will use this function to insert a new FCToken for our user.
In our App Services app, we must implement the logic of the updateFCMUserToken function that will store the token in our user document.

Function code in Atlas

We have decided to save an array of tokens to be able to send a notification to each device that the same user has used to access the application.
The following is an example of a User document in the collection:

Send notification to a topic

Firebase allows us to subscribe to a topic so that we can send a notification to all devices that have ever subscribed to it without the need to send the notification to specific device tokens.
In our application, once we have registered using an email and password, we can subscribe to receive notifications every time a new book is added or deleted.
Setting view in the iOS app
When we activate this option, what happens is that we use the Firebase SDK to register in the topic books.

How does it work?

The logic we follow will be as below:
How push notifications to topic works
In our Atlas App Services App, we will have a database trigger that will monitor the Books collection for any new inserts or deletes.
Upon the occurrence of either of these two operations, the linked function shall be executed and send a push notification to the “books” topic.
To configure this trigger, we’ll make use of two very important options:
  • Full Document: This will allow us to receive the document created or modified in our change event.
  • Document Pre-Image: For delete operations, we will receive the document that was modified or deleted before your change event.
With these options, we can determine which changes have occurred and send a message using the title of the book to inform about the change.
The configuration of the trigger in the App Services UI will be as follows:
Screenshot of the trigger configuration in the Atlas App Services UI
The function linked to the trigger will determine whether the operation occurred as an insert or delete and send the push notification to the topic books with the title information.
Function logic:
When someone adds a new book, everyone who opted-in for push notifications will receive the following:
Screenshot of a push notification on a device

Send notification to a specific device

To send a notification to a specific device, the logic will be somewhat different.
For this use case, every time a book is updated, we will search if it belongs to the favourites list of any user. For those users who have such a book, we will send a notification to all registered tokens.
This will ensure that only users who have added the updated book to their favorites will receive a notification alerting them that there has been a change.

How does it work?

Diagram of sending push notifications to a specific list of users
For this part, we will need a database trigger that will monitor for updates operations on the books collection.
The configuration of this trigger is much simpler, as we only need to monitor the updates that occur in the book collection.
The configuration of the trigger in our UI will be as follows:
Screenshot of the trigger configuration in the Atlas App Services UI
When such an operation occurs, we’ll check if there is any user who has added that book to their favorites list. If there is, we will create a new document in the pushNotifications collection.
This auxiliary collection is used to optimize the sending of push notifications and handle exceptions. It will even allow us to set up a monitoring system as well as retries.
Every time we send a notification, we’ll insert a document with the following:
  1. The changes that occurred in the original document.
  2. The FCM tokens of the recipient devices.
  3. The date when the notification was registered.
  4. A processed property to know if the notification has been sent.
Here’s an example of a push notification document:
To process the notifications, we’ll have a database trigger that will monitor the pushNotifications collection, and each new document will send a notification to the tokens of the client devices.

Function logic

Example of a push notification to a user:

Repository

The complete code for both the App Services App as well as for the iOS application can be found in a dedicated GitHub repository.
If you have found this tutorial useful, let me know so I can continue to add more information as well as step-by-step videos on how to do this.
And if you’re as excited about Atlas App Services as I am, create your first free App today!

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

How to Easily Pause and Resume MongoDB Atlas Clusters


Sep 23, 2022 | 5 min read
Tutorial

Next Gen Web Apps with Remix and MongoDB Atlas Data API


Feb 11, 2024 | 10 min read
Tutorial

How to Get MongoDB Data into Parquet in 10 Seconds or Less


Jun 28, 2023 | 5 min read
Tutorial

Building a Scalable Media Management Back End: Integrating Node.js, Azure Blob Storage, and MongoDB


Dec 13, 2023 | 10 min read
Table of Contents