<?php
//receives a topic+title+body message from an Android app and forwards it to Firebase Cloud Messaging
//FCM sends the message to other Android devices which are subscribed to the topic
//body is a JSON string - the sending service of the Android app on the originating device handles its composition and the receiving service in the apps on multiple receiving devices handle decomposition
//uses FCM Legacy HTTP and Cloud Messaging API (Legacy) Server key
//forwarded messages are currently structured for Android notifications (will use separate topic with "ios_"&topic to identify notifications for ios devices later)
//Call .php?tp=topic&t=title&b=body
$tp = $_GET["tp"];
$t = $_GET["t"];
$b = $_GET["b"];
define( 'API_KEY', 'AAA.............' );
$headers = array
(
'Authorization: key=' . API_KEY,
'Content-Type: application/json;charset=UTF-8'
);
$msg = array
(
'title' => $t,
'body' => $b,
);
$fields = array
(
'to' => '/topics/' . $tp,
'priority' => 10,
'data' => $msg
);
//forward message
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
return $result;
?>