I attempt to make a easy script to ship a push notification to a tool. One way or the other, I do know folks should “settle for” notifications within the browser, however I additionally need it to be attainable to ship the push notification on to their telephone, and never solely “within the browser”.
Can any assist me with the next code, and inform me, how I can obtain that info to maneuver ahead with my concept?
<!-- Set up Firebase ?? -->
<script sort="module">
// Import the features you want from the SDKs you want
import { initializeApp } from "
import { getAnalytics } from "
// TODO: Add SDKs for Firebase merchandise that you simply need to use
//
// Your net app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is elective
const firebaseConfig = {
apiKey: "XXXXXXX",
authDomain: "remindly-30fb7.firebaseapp.com",
projectId: "remindly-30fb7",
storageBucket: "remindly-30fb7.appspot.com",
messagingSenderId: "xxxxxxxxx",
appId: "1:632795751581:net:0075b5505db3872df9e074",
measurementId: "G-GWQCRDQDHV"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
console.log(app);
console.log(analytics);
</script>
<?php
error_reporting(E_ALL);
$API_ACCESS_KEY = "AXXXX"; //this one i obtained
$passphrase = "?????=??????????"; //Dont know what to place right here
$channelName = "?????=??????????"; //Dont know what to place right here
/** Push notification knowledge **/
$push_title = "Jesper has birthday in 7 days!!";
$push_text =
"your co-worker has birthday in 7 days - discover the proper current right here!";
operate android($reg_id)
{
international $API_ACCESS_KEY;
international $push_text;
international $push_title;
$url = "
$message = [
"title" => $push_title,
"message" => $push_text,
"subtitle" => "",
"tickerText" => "",
"msgcnt" => 1,
"vibrate" => 1,
];
$headers = [
"Authorization: key=" . $API_ACCESS_KEY,
"Content-Type: application/json",
];
$fields = [
"registration_ids" => [$reg_id],
"knowledge" => $message,
];
return useCurl($url, $headers, json_encode($fields));
}
operate WP($uri)
{
international $push_text;
international $push_title;
$delay = 2;
$msg =
"<?xml model="1.0" encoding="utf-8"?>" .
"<wp:Notification xmlns:wp="WPNotification">" .
"<wp:Toast>" .
"<wp:Text1>" .
htmlspecialchars($push_title) .
"</wp:Text1>" .
"<wp:Text2>" .
htmlspecialchars($push_text) .
"</wp:Text2>" .
"</wp:Toast>" .
"</wp:Notification>";
$sendedheaders = [
"Content-Type: text/xml",
"Accept: application/*",
"X-WindowsPhone-Target: toast",
"X-NotificationClass: " . $delay,
];
$response = useCurl($uri, $sendedheaders, $msg);
$outcome = [];
foreach (explode("n", $response) as $line) {
$tab = explode(":", $line, 2);
if (rely($tab) == 2) {
$outcome[$tab[0]] = trim($tab[1]);
}
}
return $outcome;
}
operate iOS($devicetoken)
{
international $passphrase;
$deviceToken = $devicetoken;
$ctx = stream_context_create();
// ck.pem is your certificates file
stream_context_set_option($ctx, "ssl", "local_cert", "ck.pem");
stream_context_set_option($ctx, "ssl", "passphrase", $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
"ssl://gateway.sandbox.push.apple.com:2195",
$err,
$errstr,
60,
STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT,
$ctx
);
if (!$fp) {
exit("Failed to attach: $err $errstr" . PHP_EOL);
}
// Create the payload physique
$physique["aps"] = [
"alert" => [
"title" => $push_title,
"body" => $push_text,
],
"sound" => "default",
];
// Encode the payload as JSON
$payload = json_encode($physique);
// Construct the binary notification
$msg =
chr(0) .
pack("n", 32) .
pack("H*", $deviceToken) .
pack("n", strlen($payload)) .
$payload;
// Ship it to the server
$outcome = fwrite($fp, $msg, strlen($msg));
// Shut the connection to the server
fclose($fp);
if (!$outcome) {
return "Message not delivered" . PHP_EOL;
} else {
return "Message efficiently delivered" . PHP_EOL;
}
}
operate useCurl($url, $headers, $fields = null)
{
// Open connection
$ch = curl_init();
if ($url) {
// Set the url, variety of POST vars, POST knowledge
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificates help quickly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($fields) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
}
// Execute put up
$outcome = curl_exec($ch);
if ($outcome === false) {
die("Curl failed: " . curl_error($ch));
}
// Shut connection
curl_close($ch);
return $outcome;
}
}
//Ship notification to android gadget? ((The place to seek out 'reg_id' id?? ))
android("reg_id???????? the place to seek out?");
//Ship notification to iOS gadget? (the place to seek out 'devicetoken' ?? ))
iOS("devicetoken??? the place to seek out??");
//Ship notification to Home windows gadget? (The place to seek out '$uri'? parameters? What's that?)
WP("uri, the place to seek out ???");
?>
Can any information me by means of the method to get this working? The place ought to i apply, and so on?