Send Push notification
In this tutorial, we’ll explore how to send push notifications to a user’s wallet whenever they receive a transaction containing tokens. It’s a handy way to keep users informed about their account activity.
Note: This hypothetical example is not for real-world production use. We’re simplifying things here for demonstration purposes, so there’s no thorough error handling.
We’ll be using OneSignal for sending push notifications, but you can also achieve similar functionality with other services like Firebase Cloud Messaging or AWS Pinpoint. Now, let’s dive into the details!
Step 1 - OneSignal Setup
The first step is to create a free account in OneSignal. For this example, we are going to use Web push but it works similarly for mobile.
To get started, the first step is to create a free account on OneSignal. Once you’ve signed up and logged in, we’ll proceed to create a new OneSignal App.
For this example, we’ll focus on using Web push notifications, but keep in mind that the process is quite similar for mobile apps.
Create a new OneSignal App and select Web. Once your app is created, you’ll be provided with an App ID and API Key. Keep these credentials handy as we’ll need them later to integrate OneSignal with our code.
Next, click on Configure Your Platform and select Web, select Code Custom, and set the site URL http://localhost:3000
and enable both toggles for local development.
This will generate a code snippet to add to your code. Download the OneSignal SDK files and copy them to the top-level root of your directory.
Step 2 - Frontend Setup
In a real-world scenario, your architecture typically involves customers signing up for subscriptions within your Web or Mobile App. To ensure these notifications are sent out, your app needs to register with a push notification provider such as OneSignal.
To maintain privacy and security, we’ll be using a hash of the wallet address as the externalID
instead of directly sharing the addresses with OneSignal. This externalID
will then be mapped to an address in our database. So, when our backend receives a webhook for a specific address, it can retrieve the corresponding externalID
and send a push notification accordingly.
For the sake of simplicity in our demonstration, we’ll present a basic scenario where our frontend app retrieves the wallet address and registers it with OneSignal. Additionally, we’ll simulate a database using an array within the code. Download the sample code and you’ll see client/inde.html
with this content.
Run the project using Nodejs.
Open a Chrome tab and type http://localhost:3000
, you should see something like this. Then click on Connect and accept receiving push notifications. If you are using MacOS, check in System Settings > Notifications that you have enabled notifications for the browser.
If everything runs correctly your browser should be registered in OneSignal. To check go to Audience > Subscriptions and verify that your browser is registered.
Step 3 - Backend Setup
Now, let’s configure the backend to manage webhook events and dispatch notifications based on the incoming data. Here’s the step-by-step process:
- Transaction Initiation: When someone starts a transaction with your wallet as the destination, AvaCloud webhooks detect the transaction and generate an event.
- Event Triggering: The backend receives the event triggered by the transaction, containing the destination address.
- ExternalID Retrieval: Using the received address, the backend retrieves the corresponding
externalID
associated with that wallet. - Notification Dispatch: The final step involves sending a notification through OneSignal, utilizing the retrieved
externalID
.
3.1 - Use Ngrok to tunnel the traffic to localhost
If we want to test the webhook in our computer and we are behind a proxy/NAT device or a firewall we need a tool like Ngrok. Glacier will trigger the webhook and make a POST to the Ngrok cloud, then the request is forwarded to your local Ngrok client who in turn forwards it to the Node.js app listening on port 3000. Go to https://ngrok.com/ create a free account, download the binary, and connect to your account. Create a Node.js app with Express and paste the following code to receive the webhook:
To start an HTTP tunnel forwarding to your local port 3000 with Ngrok, run this next:
You should see something like this:
3.2 - Create the webhook
The webhook can be created using the Avacloud Dashboard or Glacier API. For convenience, we are going to use cURL. For that copy the forwarding URL generated by Ngrok and append the /callbackpath
and our address.
3.3 - The backend
To run the backend we need to add the environment variables in the root of your project. For that create an .env
file with the following values:
Since we are simulating the connection to a database to retrieve the externalID, we need to add the wallet address and the OneSignal externalID to the myDB array.
The code handles a webhook event triggered when a wallet receives a transaction, performs a lookup in the simulated “database” using the receiving address to retrieve the corresponding OneSignal externalID
, and then sends an instruction to OneSignal to dispatch a notification to the browser, with OneSignal ultimately delivering the web push notification to the browser.
You can now start your backend server by running:
Send AVAX from another wallet to the wallet being monitored by the webhook and you should receive a notification with the amount of Avax received. You can try it with any other ERC20 token as well.
Conclusion
In this tutorial, we’ve set up a frontend to connect to the Core wallet and enable push notifications using OneSignal. We’ve also implemented a backend to handle webhook events and send notifications based on the received data. By integrating the frontend with the backend, users can receive real-time notifications for blockchain events.
Was this page helpful?