PHP_SMART_HOME_V3/app/class/NotificationManager.php

108 lines
2.6 KiB
PHP
Raw Normal View History

2019-10-08 14:49:08 +00:00
<?php
/**
* Notification Manager
*/
2020-01-16 13:33:11 +00:00
//TODO: Working timestamp to body or $title
2019-10-08 14:49:08 +00:00
class NotificationManager
{
function addSubscriber($userID = '', $token = ''){
$notificationSubscriber = $subDeviceId = Db::loadOne('SELECT id FROM notifications WHERE token = ?;', array($token));
if ($notificationSubscriber == ''){
$notification = array (
'user_id' => $userID,
'token' => $token,
);
Db::add ('notifications', $notification);
}
}
2020-03-12 20:34:12 +00:00
function getSubscription () {
return Db::loadAll ("SELECT * FROM notifications");
2019-10-08 14:49:08 +00:00
}
2020-01-16 13:33:11 +00:00
function sendSimpleNotification(string $serverKey, string $to, array $data, bool $timeStamp = false){
$dataTemplate = [
'title' => '',
'body' => '',
'icon' => '',
];
if (array_diff_key ($dataTemplate , $data)){
return;
}
2020-01-16 13:33:11 +00:00
if ($timeStamp) {
$data['title'] = $data['title'] . date();
}
$notification = new Notification($serverKey);
$notification->to($to);
2020-05-20 06:57:20 +00:00
$notification->notification($data['title'], date("h:i") . " - " . $data['body'], $data['icon'], '');
2019-10-11 08:43:50 +00:00
$answer = $notification->send();
$notification = null;
2019-10-11 08:43:50 +00:00
return $answer;
}
2019-10-08 14:49:08 +00:00
}
2019-10-08 14:49:08 +00:00
class Notification
{
public $server_key = '';
public $jsonPayload = [
"to" => '',
"data" => [
"notification" => [
"body" => '',
"title" => '',
"icon" => '',
"click_action" => '',
]
]
];
function __construct($serverKey = '')
{
$this->server_key = $serverKey;
}
function to($to = ''){
$this->jsonPayload["to"] = $to;
}
2020-01-16 13:33:11 +00:00
function notification($title = '', $body = '', $icon = '', $action = '', bool $timeStamp = false)
2019-10-08 14:49:08 +00:00
{
2020-01-16 13:33:11 +00:00
if ($timeStamp) {
$data['title'] = $data['title'] . date();
}
2019-10-08 14:49:08 +00:00
$this->jsonPayload["data"]["notification"]["title"] = $title;
2020-05-20 06:57:20 +00:00
$this->jsonPayload["data"]["notification"]["body"] = date("h:i") . " - " . $body;
2019-10-08 14:49:08 +00:00
$this->jsonPayload["data"]["notification"]["icon"] = $icon;
$this->jsonPayload["data"]["notification"]["click_action"] = $action;
}
function send(){
$data = json_encode($this->jsonPayload);
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$this->server_key,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if ($result === FALSE) {
die('Oops! FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
}