Push technology is one of the fastest ways to deliver notifications to users of your application. To receive them, the user does not need to perform any actions, and they are initiated on the server side and appear directly in the notification bar on the screen of the mobile device. In this lesson, we will understand how to implement push notifications in applications built on the AppMaster platform.
Send Push block
To send push notifications, Send Push blocks are used that correspond to the operating system of the target device (iOS or Android).
Getting Push Token and Bundle ID
To perform a send, you should specify at least two parameters: Push Token and IOS (Android) Bundle ID. Therefore, it is worth starting with the preparatory part and figuring out how to get these parameters.
By default, each project's database contains a Mobile Instance model that is linked to the User model and contains the required fields: push_token and bundle_id.
Let's create a separate business process for the mobile application, which will form the completed Mobile Instance model. To get a push token, use the Get Push Token block.
The next step is to get information about the device. And here, depending on the operating system, different blocks will be required. Therefore, to determine the operating system, you must use the Platform Switch block.
Depending on the result, the corresponding IOS Device Info or Android Device Info block is used. From there, you can get a variety of information about the device, but in our example, it is enough to find out only the Bundle ID.
Having collected all the data, you can form a Mobile Instance model, indicate which operating system it belongs to and pass it as an output parameter of the business process.
Recording information about the user's mobile device
The next step is to create a backend business process for connecting the generated Mobile Instance with a specific user and recording to the database. This business process requires the following:
- Find out the current user using the Auth: Get current user block.
- Expand the contents of the resulting Mobile Instance model.
- Reassemble the model and add a user connection to it using the Make Mobile Instance block.
- Use the DB: Create Mobile Instance block and save the data in the database.
In the POST /Mobile Instance/ endpoint, you can replace the standard business process with the one that was just created.
The final stage of preparation is to decide when exactly it is worth writing information about the user's device into the database. The logical solution would be to do this when performing authorization in the application. To do this, immediately after the Set Auth Token block, two new blocks with previously created business processes must be added to the user authorization process in the mobile application. The first one receives and generates the necessary information, and the second creates a connection with the user and writes to the database.
Sending push notifications
The preparatory part is completed, and you can start sending push notifications. To do this, we will create a backend business process. Imagine that our task will be to send a notification to all users of our application. We will send them only to devices with the Android operating system and not to all the user's devices, but only to the one that was used last.
Let's start with the input parameters. It is necessary to transfer the text of the notification itself. It consists of three parts: the body of the message (body), its title (title), and subtitle (subtitle).
Next, you need to find all users of the application. To do this, use the Search User block and set the necessary parameters:
- _With = Mobile Instances (you need to get not only information about the user, but also about the mobile device associated with it)
- Limit = -1 (by default, the query returns 25 records, “-1” is specified to remove this limit and get all users)
- _mobile_instances_os = Android (according to the conditions of the task, we are only interested in Android devices)
For the generated array of users, you need to run a cycle of operations (For each loop block)
For each user from the Expand User block, you need to get information about mobile devices (Mobile Instances). We need to send a notification to only one device, so we set index = 0 in the Array Element block and also check if the array contains at least one element at all (after all, a user can only have an iOS device). To do this, we check the element for validity, pass the valid index value to the If-Else block, and continue the loop only if the value is true.
If everything is correct and information about the user's device is found, then you need to use the Expand Mobile Instance block and pass the necessary values to the Send Push (Android) block.
Please note that when developing and testing an application in the Developer App, you must set the Developer Mode = True.
You can also use the TTL (Time to live) option. It defines the time in seconds for the notification to be sent. If the notification cannot be delivered immediately, for example, if the user is temporarily out of range, then repeated delivery attempts will be made at the allotted time.
At the end of the cycle and sending notifications to all possible users, the work of the business process is also completed.
Separately, it is worth discussing additional parameters of the send block:
- Push ID
- Push Class
They do not affect the fact of sending a notification or its content but can be used when processing a notification on the user's side. The mobile application has special triggers that are fired when a received push notification is received or clicked.
Push notification triggers contain data from the above additional text parameters. As an example, we can give the simplest process, which, when the trigger is clicked, will display a further notification on the screen with the information passed in the Data parameter.
Another popular use case for triggers is to collect information about the delivery of notifications. For example, they can send a message to the server stating that a Push notification has indeed been delivered, as well as collect statistics and count the number of clicks on notifications.