Build your Own Notification System
This guide helps you build your own Notification System through making use of Gameball Notifications Webhook.
Notifications play a huge role in strengthening the bond between you and your customers (a.k.a. players) as it helps them remain engaged with your service.
Gameball offers you some default ways for sending notifications to your players such as having in-app notifications for web applications, and push notifications by integrating with firebase for mobile applications.
But what if you have your own notification system that you use to contact your players. Maybe an SMS messaging system or a WhatsApp bot you own or any other services you use for this purpose such as firebase messaging, Amazon SNS or Twilio.
Gameball uses webhooks to notify your application when an event happens and a notification is needed to be delivered to your player.
The webhook endpoint is just more code on your server, which could be written in Ruby, PHP, Node.js, or whatever you use. The webhook endpoint has an associated URL (e.g., https://yourdomain.com/webhookEndPoint ).
You will provide Gameball with your endpoint URL and whenever a player should receive a notification, a POST request will be sent to your endpoint with required data in the request body.
In this tutorial we will walkthrough how to set up your own notification system with Gameball.
To begin using notification webhooks with Gameball you should follow these steps:
  1. 1.
    Build a notification webhook endpoint on your server.
  2. 2.
    Subscribe to the notification webhook in your dashboard.
  3. 3.
    Verify the webhook signature.
  4. 4.
    Build your own logic for sending notifications to your player.
Code snippets used here are just for illustrations and they might not actually work.
Let's assume that you own a store that excels in selling video games and gift cards for gamers all over the world. You want to send notifications to your players via your own notification channels that you use to communicate with your players.

Configurations

There are some configuration steps that you need to do to set up your way for having webhooks.

Step 1: Build Notifications Endpoint On Your Server

First of all, you will need to prepare an endpoint on your server that Gameball notification webhook will POST generated notifications to.
1
app.post('/GameballHooks/notification', (request, response) => {
2
โ€‹
3
});
Copied!
The above function will be invoked when a POST request is sent to /GameballHooks/notification.

Step 2: Activate Notifications Webhook

In your Gameball dashboard, navigate to Admin Settings > Account Integration > Gameball Webhooks and add your notification endpoint URL (www.yourdomain.com/GameballHooks/notification in this example) to Notification Webhook.
and then click Update Webhook. Now, a POST request will be sent to your endpoint every time a notification is to be sent to your player.

Step 3: Verifying Webhooks

You should verify that the incoming request is a legit request from Gameball and not a malicious request that tries to spam your system. You can achieve this by calculating a digital signature which is BASE64 encoded in X-GB-Signature header. You can get more information about webhook verification in this section.
1
app.post('/GameballHooks/notification',(request,response) => {
2
// verify the request
3
if(verifyWebhook(request) == false) return; // malicious request
4
});
Copied!

Build Your Own Logic

Step 4: Write Your Own Logic

In the webhook request body, you will get all the needed data for you to send your customized notifications designated players. Each notification item in the posted data will contain the playerUniqueId and locales with their titles and bodies. You can check the full data specification and example in the documentation here.
Here is an example of a notification data:
1
{
2
"notifications":[
3
{
4
"notificationId" : "123",
5
"title": "New level!",
6
"body": "Keep it up! You are now on Bronze ",
7
"isRead": true,
8
"createdAt": "2021-05-12T00:08:09.646174",
9
"lang": "en",
10
"icon": "https://cdn.gameball.co/uploads/client776/ad8b2587-959f-48fd-ab58-a643323652begb-icon-level-13.png"
11
}
12
]
13
...
14
}
Copied!
After receiving the notification, you can process it according to your preferences and you should have the flexibility of building whatever logic you need.
The below example demonstrates how SMS integration can be done with notification webhooks. Once a notification event is received, you will find the playerUniqueId in the request body. You can use the received playerUniqueId it to retrieve your player's mobile number or any other required data from your database, then use an SMS Gateway to send an SMS to your player.
1
app.post('/GameballHooks/notification',(request,response) => {
2
// verify the request
3
if(verifyWebhook(request) == false) return; // malicious request
4
โ€‹
5
let notificationData = request.body;
6
โ€‹
7
/* Example: {userId:"14415233", username:"XxGamerxX", phone: "+20123456789", deviceToken: "dSvsjGMuQ5GwcAYPAqu4PW:APA91bFzRjdiTj" } */
8
let playerData = getPlayerData(notificationData.playerUniqueId);
9
10
// sends an SMS to your player's phone number
11
SMSGateway.sendMessage({
12
phoneNumber: playerData.phone,
13
messageTitle: notificationData.notifications[0].title,
14
messageContent: notificationData.notifications[0].body
15
});
16
});
Copied!
And now your players should receive an SMS message when they get a notification from Gameball.

โ€‹

Whatsapp Example

The next day your boss requested a slight change, that you need to also send WhatsApp messages to your players in addition to sending SMSes.
You will need to have an integration with WhatsApp business API or any communication API as Twilio.
In your โ€œ/GameballHooks/notificationโ€ endpoint you will add the logic that sends WhatsApp message to your player.
1
app.post('/GameballHooks/notification',(request,response) => {
2
// verify the request
3
if(verifyWebhook(request) == false) return; // malicious request
4
โ€‹
5
let notificationData = request.body;
6
โ€‹
7
/* Example: {userId:"14415233", username:"XxGamerxX", phone: "+20123456789", deviceToken: "dSvsjGMuQ5GwcAYPAqu4PW:APA91bFzRjdiTj" } */
8
let playerData = getPlayerData(notificationData.playerUniqueId);
9
10
// sends an SMS to your player's phone number
11
SMSGateway.sendMessage({
12
phoneNumber: playerData.phone,
13
messageTitle: notificationData.notifications[0].title,
14
messageContent: notificationData.notifications[0].body
15
});
16
โ€‹
17
// sends a Whatsapp message to your player
18
WhatsappHandler.sendWhatsappMessage({
19
phoneNumber: playerData.phone,
20
messageContent: notificationData.notifications[0].body
21
})
22
});
Copied!
โ€‹
โ€‹

AWS SNS Push Notification Example

In addition to SMS and Whatsapp you are asked to send push notifications to your playersโ€™ mobile devices as push notifications. There are some services that provide this service, we will use AWS SNS for this example.
AWS SNS uses device tokens to identify mobile devices. You store each player's device token in your database.
In your โ€œ/GameballHooks/notificationโ€ endpoint you will add the logic that sends push notification to your player mobile device.
1
app.post('/GameballHooks/notification',(request,response) => {
2
// verify the request
3
if(verifyWebhook(request) == false) return; // malicious request
4
โ€‹
5
let notificationData = request.body;
6
โ€‹
7
/* Example: {userId:"14415233", username:"XxGamerxX", phone: "+20123456789", deviceToken: "dSvsjGMuQ5GwcAYPAqu4PW:APA91bFzRjdiTj" } */
8
let playerData = getPlayerData(notificationData.playerUniqueId);
9
10
// sends an SMS to your player's phone number
11
SMSGateway.sendMessage({
12
phoneNumber: playerData.phone,
13
messageTitle: notificationData.notifications[0].title,
14
messageContent: notificationData.notifications[0].body
15
});
16
โ€‹
17
// sends a Whatsapp message to your player
18
WhatsappHandler.sendWhatsappMessage({
19
phoneNumber: playerData.phone,
20
messageContent: notificationData.notifications[0].body
21
})
22
โ€‹
23
// sends a push notification
24
SNS.sendNotification({
25
token: playerData.deviceToken,
26
title: notificationData.notifications[0].title,
27
body: notificationData.notifications[0].body
28
})
29
});
Copied!
By the end of this tutorial you should now be able to implement your own notification system using notifications hooks.
Last modified 2mo ago