Back

Tutorials

Mar 19, 2024

How to send iOS critical alerts using FCM

Jobin Jose

Critical Alerts are special type of notifications that bypass the device's DND mode, mute settings to generate an audible notification in emergency situations. It requires special approval from Apple to send critical alerts from your app to your users. In this tutorial, we'll learn how to send critical alerts to iOS apps using FCM (Firebase Cloud Messaging) and APNS.

Prerequisites for sending critical alerts to iOS


  1. Entitled to send critical alerts by Apple (Read more)

  2. Engagespot account (Get one free here)

  3. Google Firebase Cloud Messaging (FCM) configured with APNS (Read more)

1. Activating the FCM provider in the Engagespot console

The first step involves enabling the FCM provider within your Engagespot console. Here's how you can do it.


  • Navigate to the `Channels -> Mobile Push` section in your Engagespot dashboard.

  • Activate the FCM provider.

  • Paste the contents of your Service Account JSON file, which can be procured from your Firebase Settings. Read this guide to learn how to setup FCM with Engagespot.

2. Synchronizing user profiles with FCM tokens

To send push notifications to your users, they must be registered with Engagespot, and their FCM token must be updated in their user profile. The Engagespot API can be used to add FCM tokens to user profiles.

If you're unfamiliar with the Users concept in Engagespot, reading this guide will be helpful.

Here's an example of how to attach FCM token to your user's profile using cURL. (This is usually done directly from your app if you're using our In-App Inbox library, or your backend).

Make sure NOT to use ENGAGESPOT_API_SECRET in your front-end apps. You should use User token based authentication instead.

curl --location --request POST 'https://api.engagespot.co/v3/profile' \
--header 'X-ENGAGESPOT-API-KEY: YOUR_ENGAGESPOT_API_KEY' \
--header 'X-ENGAGESPOT-API-SECRET: YOUR_ENGAGESPOT_API_SECRET' \
--header 'Content-Type: application/json' \
--data-raw '{
    "fcm":{
        "token":"YOUR_FCM_TOKEN"
    }
}'

Remember to replace YOUR_ENGAGESPOT_API_KEY, YOUR_ENGAGESPOT_API_SECRET, and YOUR_FCM_TOKEN with your actual Engagespot API key, secret, and FCM token (obtained from your mobile app) respectively.

3. Dispatching critical alerts via the Engagespot API

You can use our Send Notification REST API or one of our client libraries for Node, PHP, Python etc to trigger notifications from your backend. In the override parameter, use the fcm key to pass the custom request payload that needs to be sent to the FCM API.

Since critical alerts is not a native feature supported by Engagespot, we're making use of the override functionality of our API to send additional payload directly to FCM as per their documentation to send critical alerts.

Here's a Python example:

import requests
url = "https://api.engagespot.co/v3/notifications"
payload = {
    "title": "Critical Alert!",
    "message": "Urgent action required.",
    "override": {
        "fcm": {
             "apns": {
                "payload": {
                    "aps": {
                        "criticalSound": {
                            "critical": True,
                            "name": 'default',
                            "volume": 1.0,
                        },
                    },
                },
            },
        }
    }
}
headers = {
    "X-ENGAGESPOT-API-KEY": "YOUR_ENGAGESPOT_API_KEY",
    "X-ENGAGESPOT-API-SECRET": "YOUR_ENGAGESPOT_API_SECRET",
    "Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)

You can also override any FCM Payload using the `override.fcm` feature. For more details, refer to the [Firebase documentation](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages/send).

4. Tailoring FCM parameters for critical alerts

The `override.fcm` parameter allows you to customize FCM parameters such as priority and additional payload data. This ensures that critical alerts are delivered promptly and contain relevant information. Here's an example of how to override FCM parameters:

"override": {
    "fcm": {
        "data": {
            "custom_key": "custom_value"
        }
    }
}

Conclusion

By adhering to these steps and utilizing the provided code examples, you can effectively dispatch critical alerts using FCM in Engagespot. The customization of FCM parameters ensures that your critical notifications are delivered promptly and carry the necessary information. This not only enhances user engagement but also prompts timely action when it's most needed. Leveraging the power of FCM with Engagespot can significantly elevate your user engagement strategy.

Happy coding!

Jobin Jose

Share this post