Send the Notification
Prerequisite
NotificationAPI account and a configured notification in the dashboard. If you don't have an account, you can sign up for free.
1. Install back-end SDK
Notifications are triggered from your back-end. So install one of our back-end SDKs:
- JavaScript
- Python
- PHP
- Go
- C#
- Ruby
npm install notificationapi-node-server-sdk
// or using yarn:
yarn add notificationapi-node-server-sdk
pip install notificationapi_python_server_sdk
composer require notificationapi/notificationapi-php-server-sdk
go get github.com/notificationapi-com/notificationapi-go-server-sdk
// Create the followinng class in your application:
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
class NotificationAPI {
private string baseURL = "https://api.notificationapi.com";
private string clientId;
private HttpClient httpClient;
public NotificationAPI(string clientId, string clientSecret) {
this.clientId = clientId;
string authToken = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{clientId}:{clientSecret}"));
this.httpClient = new HttpClient();
this.httpClient.DefaultRequestHeaders.Add("Authorization", $"Basic {authToken}");
}
public async Task<string> send(string request) {
HttpContent payload = new StringContent(request, Encoding.UTF8, "application/json");
var response = await this.httpClient.PostAsync($"{this.baseURL}/{this.clientId}/sender", payload);
var responseContent = await response.Content.ReadAsStringAsync();
return responseContent;
}
public async Task<string> retract(string request) {
HttpContent payload = new StringContent(request, Encoding.UTF8, "application/json");
var response = await this.httpClient.PostAsync($"{this.baseURL}/{this.clientId}/sender/retract", payload);
var responseContent = await response.Content.ReadAsStringAsync();
return responseContent;
}
}
# Create the following class in your application
require 'net/http'
require 'json'
require 'base64'
class NotificationAPI
def initialize(client_id, client_secret)
@base_url = 'https://api.notificationapi.com'
@client_id = client_id
@auth_token = Base64.strict_encode64("#{client_id}:#{client_secret}")
@http_client = Net::HTTP.new('api.notificationapi.com', 443)
@http_client.use_ssl = true
end
def send(request)
payload = request.to_json
response = @http_client.post(
"/#{@client_id}/sender",
payload,
{
'Content-Type' => 'application/json',
'Authorization' => "Basic #{@auth_token}"
}
)
response.body
end
def retract(request)
payload = request.to_json
response = @http_client.post(
"/#{@client_id}/sender/retract",
payload,
{
'Content-Type' => 'application/json',
'Authorization' => "Basic #{@auth_token}"
}
)
response.body
end
end
2. Send
The example below sends the "New Comment Notification" to the test user:
- JavaScript
- Python
- PHP
- Go
- C#
- Ruby
// import/require:
import notificationapi from 'notificationapi-node-server-sdk';
// const notificationapi = require('notificationapi-node-server-sdk').default
// init
notificationapi.init('CLIENT_ID', 'CLIENT_SECRET');
// send
notificationapi.send({
notificationId: 'new_comment_notification',
user: {
id: 'TEST_USER_ID',
email: 'TEST@TEST.COM', // required for email notifications
number: '+15005550006' // required for SMS
}
});
# import
from notificationapi_python_server_sdk import (notificationapi)
# init
notificationapi.init("CLIENT_ID", "CLIENT_SECRET")
# send
notificationapi.send({
"notificationId": "new_comment_notification",
"user": {
"id": "TEST_USER_ID",
"email": "TEST@TEST.COM", # required for email notifications
"number": "+15005550006" # required for SMS
}
})
# import
use NotificationAPI\NotificationAPI;
# init
$notificationapi = new NotificationAPI('CLIENT_ID', 'CLIENT_SECRET');
# send
$notificationapi->send([
"notificationId" => "new_comment_notification",
"user" => [
"id" => "TEST_USER_ID",
"email" => "TEST@TEST.COM", # required for email notifications
"number" => "+15005550006" # required for SMS
]
]);
package main
// import
import (
notificationapi "github.com/notificationapi-com/notificationapi-go-server-sdk"
)
func main() {
// init
notificationapi.Init("CLIENT_ID", "CLIENT_SECRET")
// send
params := notificationapi.SendRequest{NotificationId: "new_comment_notification", User: notificationapi.User{
Id: "TEST_USER_ID",
Email: "TEST@TEST.COM", // required for email notifications
Number: "+15005550006", // required for SMS and call
},
}
notificationapi.Send(params)
}
NotificationAPI notificationapi = new NotificationAPI("CLIENT_ID", "CLIENT_SECRET");
string request = @"{
""notificationId"": ""hello_world"",
""user"": {
""id"": ""123"",
""email"": ""test@test.com"",
""number"": ""+15005550006""
},
""mergeTags"": {
""firstName"": ""FIRST_NAME""
}
}";
notificationapi.send(request);
# require:
require './NotificationAPI'
# init
notificationapi = NotificationAPI.new("CLIENT_ID", "CLIENT_SECRET")
# send
notificationapi.send({
notificationId: 'new_comment_notification',
user: {
id: 'TEST_USER_ID',
email: 'TEST@TEST.COM', # required for email notifications
number: '+15005550006' # required for SMS
}
});
info
You must replace the CLIENT_ID and CLIENT_SECRET with real values. You can get yours from here.
That's it?
You are now sending notifications through email, SMS, automated voice calls, etc.
Please take the time to review:
- In-App Notifications require our Front-End SDK
- Free usage tier
- SMS/Voice Details