<?php
$url = "https://fcm.googleapis.com/fcm/send";
$topico = "general";
$api_key = "AAAA9Jyxxxxxxxxxx"; //key server personale
$titulo = "Prima Notifica";
$corpo = "Messaggio n 1";
$legenda = "Notifica";
$headers = array
(
'Authorization: key='.$api_key,
'Content-Type: application/json;charset=UTF-8'
);
$data = array
(
'data' =>
array (
'title' => $titulo,
'body' => $corpo,
'subtitle' => $legenda,
),
'to' => '/topics/'.$topico,
'priority' => 'high',
//'restricted_package_name' => 'com.onlyoneapp.test', //IF YOU WANT SEND TO ONLY ONE APP
);
$content = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false );
$result = curl_exec($curl);
curl_close($curl);
$arr = array();
$arr = json_decode($result,true);
if ($arr === FALSE) {
echo "Json invalido!"."<br>";
} else if (empty($arr)) {
echo "Json invalido!"."<br>";
}else{
if (array_key_exists ('message_id', $arr)){
echo "Messaggio Inviato! <br>Mensagem id: ".$arr['message_id']."<br>";
}else{
echo "Ocorreu um erro ao enviar a notificação!"."<br>";
}
}
?>