Delivery Push Notifications
When a message is intially submitted, the messaging server immediately returns a response data which we have covered so far. This response data includes the identifier assigned to the submitted batch as well as identifiers for each added destination and their status. However, this information does not indicate that the message has been delivered to added destinations. The mobile operator returns the delivery status of each added destination as and when they are delivered to the mobile phones.
Applications can request that when mobile operators return the delivery status of each destination, the messaging server also pushes the delivery status to applications's callback URL so that the client application processes the response.
In this section, we will look at how applications can set a delivery callback URL in the message data on which delivery status received from the mobile operators will be pushed to.
Setting Delivery Callback
To be notified of message delivery status received from mobile operators, applications will need to set a callback URL and the content type to be received in the message data before sending the request:
The following is an example SMS data with delivery callback information set:
{
"text": "Hello World!",
"type": 0,
"sender": "TEST",
"destinations": ["233242053072", "0246314915"],
"callback": {
"url": "https://message_callback_url/handler",
"accept": "application/json"
}
}
Notice the inclusion of the callback
property in the message data. The
callback
property contains an object consisting of url
and
accept
properties. When delivery status is received from mobile operator, the status will
be pushed to the specified URL in the content type format set to the accept
property. The value for the accept
property must either be application/json
or application/xml
.
The following is a sample application code that sets the delivery callback information in SMS message data:
<?php
// set up the request headers
$headers = [
'Host: api.smsonlinegh.com',
'Content-Type: application/json',
'Accept: application/json',
'Authorization: key d5c683a1b4c3d2f278be3d4c03c23191b2f133378b12b6e197c1ad5d9b34c128'
];
// set up the message data
$messageData = [
'text'=>'This is a test message',
'type'=> 0, // GSM default
'sender'=> 'TEST',
'destinations'=> ['0246314915', '0242053072']
];
// set delivery callback information
$messageData['callback']['url'] = 'https://delivery_callback_domain/handler';
$messageData['callback']['accept'] = 'application/json';
// initialise cURL
$ch = curl_init('https://api.smsonlinegh.com/v5/sms/send');
// set cURL optionS
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($messageData));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute for response
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// close curl
curl_close($ch);
if ($httpCode == 200){
var_dump($response);
}
Note
The value set as sender
must exist in the list of user message sender names. If the
name has not been added in user message sender names, then the message will not be submitted.
The sender name must be requested from the user account under SMS Messaging menu option.
Delivery Callback Response
A delivery callback response is similar to the response data in a Message Delivery Report. The only difference is that the response data for a delivery push notification applies to a single message destination. As a result, a delivery push response data will have a single phone number information in the destinations section.
The following data shows an example of a response data for a delivery push notification for SMS submitted:
{
"handshake" : {
"id" : 0,
"label" : "HSHK_OK"
},
"data": {
"batch" : "cfa19ba67f94fbd6b19c067b0c87ed4f",
"delivery": true,
"category": "sms",
"text": "Hello world!",
"type": 0,
"sender": "Hello",
"personalised": false,
"destinationsCount": 2,
"destinations: [
{
"to" : "233246314915",
"id" : "093841e5-578a-41f4-5f5f-2f3910886c12",
"country": "Ghana",
"messageCount": 1,
"submitDateTime":"2021-09-29 21:57:44",
"reportDateTime":"2021-09-29 21:57:48",
"status" : {
"id" : 2110,
"label" : "DS_DELIVERED"
}
}
]
}
}