// Check if form data is received
if (isset($_REQUEST['tag'])) {
$tag = htmlspecialchars(trim($_REQUEST['tag'])); // Sanitize input
$client_type = htmlspecialchars(trim($_REQUEST['client_type'])); // Sanitize input
$is_voip_push = true; // Set to true for VOIP push
// $client_type = "FcmV1"; // Platform type (e.g., FcmV1, apple, etc.)
// Prepare notification details
$title = "New Alert";
$body = "You have a new update. Please check your app for details.";
$uuid = Uuid::uuid4();
// Convert the UUID to string and remove hyphens
$message_id = str_replace('-', '', $uuid->toString());
if($client_type == "android"){
$payload = json_encode([
"notification" => [
"title" => $title,
"body" => $body,
],
"android" => [
"priority" => "high" // Example custom data
]
]);
}
if($client_type == "apple"){
$aps = [
"content-available" => 1,
"alert" => [
"title" => $title,
"body" => $body
]
];
$payload = json_encode([
"aps" => $aps
]);
}
// Instantiate NotificationHub
try {
$notificationHub = new NotificationHub($connectionString, $hubName);
echo $payload;
echo "\n";
// Create Notification object
$notification = new Notification($client_type, $payload);
// Send Notification
$message_id = $notificationHub->sendNotification($notification, $tag, $is_voip_push, $client_type);
// Log result
if ($message_id) {
echo "Notification sent successfully! Message ID: $message_id\n";
} else {
echo "Failed to send notification.\n";
}
} catch (Exception $e) {
echo "Error sending notification: " . $e->getMessage() . "\n";
}
}
Main Sending Method
public function sendNotification($notification, $tagsOrTagExpression, $is_voip_push, $client_type)
{
if (is_array($tagsOrTagExpression)) {
$tagExpression = implode(" || ", $tagsOrTagExpression);
} else {
$tagExpression = $tagsOrTagExpression;
}
# build uri
$uri = $this->endpoint . $this->hubPath . "/messages" . NotificationHub::API_VERSION;
//$url = "https://fcm.googleapis.com/fcm/send";
// $GLOBALS['log']->info("apiv2:NotificationHub:info:" . ' uri: ' . $uri);
$message_id = null;
$ch = curl_init($uri);
if (in_array($notification->format, ["template", "apple", "fcm", "gcm", "FcmV1", "adm"])) {
$contentType = "application/json;charset=utf-8";
} else {
$contentType = "application/xml";
}
$token = $this->generateSasToken($uri);
$headers = [
'Authorization: '.$token,
'Content-Type: '.$contentType,
'ServiceBusNotification-Format: '.$notification->format
];
if ("" !== $tagExpression) {
$headers[] = 'ServiceBusNotification-Tags:' .$tagExpression; #;
}
if ($is_voip_push && ($client_type == "apple" || $client_type == "iphone") ) {
$headers[] = 'ServiceBusNotification-Apns-Push-Type: voip';
$headers[] = 'ServiceBusNotification-Apns-Topic: com.messaging.stars.voip';
$headers[] = 'ServiceBusNotification-Apns-Priority: 10';
$headers[] = 'ServiceBusNotification-Apns-Expiration: 0';
$GLOBALS['log']->info("apiv2:NotificationHub:Headers of sending notificaiton:" . json_encode($headers));
print_r($headers);
}
# add headers for other platforms
if (is_array($notification->headers)) {
$headers = array_merge($headers, $notification->headers);
}
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $notification->payload,
CURLOPT_HEADER => TRUE, // Include headers in the output
CURLOPT_VERBOSE => TRUE
));
curl_setopt($ch, CURLOPT_VERBOSE, true);
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
$GLOBALS['log']->info("apiv2:NotificationHub:Error sending notificaiton:" . curl_error($ch));
return;
}
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$info = curl_getinfo($ch);
$GLOBALS['log']->info("apiv2:NotificationHub:status_code:" . $info['http_code'] . ' msg: ' . $body);
$GLOBALS['log']->info("apiv2:NotificationHub:info: headers: " . $header);
if ($info['http_code'] <> 201) {
$GLOBALS['log']->info("apiv2:NotificationHub:Error sending notificaiton:" . $info['http_code'] . ' msg: ' . $response);
return;
}
// Extract Location header
$headers_array = explode("\r\n", $header);
$location_header = null;
foreach ($headers_array as $header_line) {
if (stripos($header_line, 'Location:') === 0) {
$location_header = trim(substr($header_line, 9));
break;
}
}
try{
$parsed_url = parse_url($location_header);
$path = $parsed_url['path'];
$path_segments = explode('/', $path);
$message_id = end($path_segments);
}catch (Exception $e) {
$GLOBALS['log']->info("apiv2:NotificationHub:error: " . $e->getMessage());
return;
}
return $message_id;
}
Registration Request
<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<content type="application/xml">
<AppleRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
<Tags>keith51330_20241128132110</Tags>
<DeviceToken>---replaced value --XXXX</DeviceToken>
</AppleRegistrationDescription>
</content>
</entry>
Registration Response
<entry p1:etag="W/"1"" xmlns:p1="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<id>https://xx.notificationhub.windows.net/xx-01/registrations/3722865485130199588-5427083653015844075-1?api-version=2015-01</id>
<title type="text">3722865485130199588-5427083653015844075-1</title>
<published>2024-11-28T07:51:18Z</published>
<updated>2024-11-28T07:51:18Z</updated>
<link rel="self" href="https://xxx.windows.net/xx-01/registrations/3722865485130199588-5427083653015844075-1?api-version=2015-01" />
<content type="application/xml">
<AppleRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
<ETag>1</ETag>
<ExpirationTime>9999-12-31T23:59:59.9999999</ExpirationTime>
<RegistrationId>3722865485130199588-5427083653015844075-1</RegistrationId>
<Tags>keith51330_20241128132110</Tags>
<DeviceToken>xxx</DeviceToken>
</AppleRegistrationDescription>
</content>
</entry>
This is the last try please let me suggest if i am doing anything wrong Normal push is working but voip push is not working