ClickCease Tracking
<greylight>
Copy
Amity real-time events

Create a Webhook URL to subscribe to Amity real-time events

Mark Worachote
Solutions Engineer
Android
iOS
Web
Jun 11, 2022

Create a Webhook URL to subscribe to Amity real-time events with Node.js & send push notifications to iOS + Android devices & Web browsers

Push notifications are a crucial part of any mobile or web application. They enable you to send real-time updates to your users, even when they're not actively using your app. In this tutorial, we will guide you on how to create a Webhook URL to receive real-time events and send push notifications to iOS and Android devices. We'll be using Node.js, Express, Firebase Cloud Messaging (FCM), and Apple Push Notification service (APNs) to accomplish this.

Pre-requisites

Before we begin, you'll need the following:

  1. Node.js and npm installed on your machine.
  2. A Firebase project set up for Android push notifications. You can create one at the Firebase console.
  3. Apple Developer account for iOS push notifications.
  4. An Amity Social Cloud account with a Chat SDK set up.

Step 1: Setting Up Your Node.js Server

First, let's set up a simple Node.js server that will act as our Webhook URL. We'll use Express.js for this:

Step 2: Subscribing to Amity Events

Now, let's configure Amity Social Cloud to send events to our Webhook URL. You'll do this in the Amity console. Navigate to your app settings, find the Webhook section, and enter your Webhook URL.

Step 3: Handling Amity Events

Now, let's update our server to handle the Amity events and send push notifications:

Step 4: Configuring the iOS App

For iOS, you'll need to configure your app to receive push notifications. This involves enabling push notifications in your app's capabilities, registering for remote notifications, and handling incoming notifications. You'll also need to set up APNs in the Apple Developer console and add the necessary configurations to your Xcode project.

Step 5: Configuring the Android App

For Android, you'll need to add the Firebase SDK to your project, configure Firebase in your app, and handle incoming messages. You'll also need to add the necessary permissions to your AndroidManifest.xml and create a service to handle FCM messages.

Step 6: Testing Your Setup

To test your setup, you can trigger a message.created event in Amity and check if your Node.js server receives the event and sends the push notification correctly. You can also use tools like Postman to manually send a POST request to your Webhook URL and check the server's response.

Step 7: Sending Web Push Notifications

For web push notifications, you'll need to set up a service worker in your web app and use the Push API. Here's a basic example:

Step 8: Handling Web Push Notification Subscriptions

To send push notifications to a user's browser, you need the user's push subscription object. You can get this by subscribing the user to push notifications when they first visit your site:

Step 9: Sending Web Push Notifications from the Server

On the server side, you can use the web-push library to send push notifications:

Final Thoughts

By following this guide, you've set up a Webhook URL to subscribe to Amity real-time events and send push notifications to iOS and Android devices. This setup allows you to keep your users engaged with real-time updates, even when they're not actively using your app.

Remember, the key to effective push notifications is relevance and timing. Make sure your notifications are providing value to your users and not causing notification fatigue. Also, always handle the user's permission to send notifications gracefully and respect their decision.

If you found this tutorial useful and want to explore more about the possibilities with Amity Social Cloud, feel free to visit our tutorials page. We have a variety of tutorials that cover a wide range of use cases as well such as Analytics! which can really help giving you a much more in-depth insights on your platform.

If you're interested to learn more about Amity and what we offers, you can take a look at our website or you can contact us here at our contact page. Looking forward to talk to you soon!