Permalink
Browse files

HTML5 push notifications

  • Loading branch information...
r04r committed May 16, 2015
1 parent edea191 commit 5d8db3b032199854958815d60820d5ff11e76022
View
@@ -70,6 +70,7 @@ RewriteRule ^watchlist$ watchlist.php [L]
RewriteRule ^folks$ folks.php [L]
RewriteRule ^stuff$ stuff.php [L]
RewriteRule ^triptest$ triptest.php [L]
RewriteRule ^push$ push.php [L]
# New reply and topic stuff.
RewriteRule ^new_reply/([0-9]+)/([a-z0-9]*)$ post.php?reply=$1&token=$2 [L]
View
BIN +88 Bytes chrome-push/favicon.gif
Binary file not shown.
View
@@ -0,0 +1,13 @@
{
"name": "Minichan Push",
"short_name": "Minichan Push",
"icons": [{
"src": "favicon.gif",
"sizes": "16x16",
"type": "image/gif"
}],
"start_url": "/",
"display": "standalone",
"gcm_sender_id": "254173585206",
"gcm_user_visible_only": true
}
@@ -1,5 +1,5 @@
<?php
define('DB_VERSION', 1); // Version of the database that the code expects
define('DB_VERSION', 2); // Version of the database that the code expects
class db{
protected $db_link = NULL;
@@ -0,0 +1,14 @@
<?php if(!$upgrade) die();
multi_query(<<<SQL
CREATE TABLE IF NOT EXISTS `chrome_tokens` (
`uid` varchar(23) NOT NULL,
`subscription_id` varchar(256) NOT NULL,
`endpoint` varchar(2048) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `chrome_tokens`
ADD KEY `uid` (`uid`), ADD KEY `subscription_id` (`subscription_id`), ADD KEY `endpoint` (`endpoint`(767));
ALTER TABLE `chrome_tokens` ADD PRIMARY KEY( `uid`, `subscription_id`);
SQL
);
View
@@ -106,6 +106,8 @@ function add_notification($event, $target, $identifier, $data, $parent_id = null
//_send_notification('put', array('event'=>$event, 'target'=>$target, 'identifier'=>$identifier, 'data'=>json_encode($data), 'parent_id'=>$parent_id));
sendMessageToChrome($target, $event, $data);
$link->db_exec("SELECT token FROM android_tokens WHERE uid = %1", $target);
if($link->num_rows() > 0) {
$result = $link->fetch_assoc();
@@ -398,6 +400,51 @@ function print_statistics($uid, $public=true) {
<?php
}
function sendMessageToChrome($uid, $type, $data) {
global $link;
if(!defined('CHROME_TOKEN')) return;
// The push events don't support data yet
$subscriptions = array();
$link->db_exec("SELECT subscription_id FROM chrome_tokens WHERE uid = %1", $uid);
while(($row = $link->fetch_assoc()) != null) {
$subscriptions[] = $row['subscription_id'];
}
if(count($subscriptions) == 0) return;
var_dump($subscriptions);
$headers = array(
'Authorization: key=' . CHROME_TOKEN,
'Content-Type: application/json'
);
$post = array(
"registration_ids" => $subscriptions
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$response = curl_exec($ch);
var_dump(curl_error($ch));
curl_close($ch);
var_dump($response);
return $response;
}
function sendMessageToPhone($deviceRegistrationId, $msgType, $extraParams) {
global $link;
View
@@ -31,6 +31,16 @@ function console($str, $state=null) {
echo "[$state] $str\n";
}
function multi_query($sql) {
global $link;
$sql = preg_split('/;\s*$/m', $sql);
foreach($sql as $query) {
if(trim($query) == "") continue;
console("Performing query...");
$link->db_exec($query);
}
}
if($tableCount === 0) {
$logState="init";
console("Loading tables");
View
@@ -5,7 +5,10 @@
$cite_topics = array();
if($new_citations) {
if($num_pms > 0) {
header('Location: ' . DOMAIN . 'private_message/' . $_SESSION['pm_id']);
die();
}elseif($new_citations) {
echo '<h2>Citations</h2>';
// Delete notifications of replies-to-replies that no longer exist.
$link->db_exec('DELETE FROM citations WHERE uid = %1 AND (NOT EXISTS (SELECT 1 FROM replies WHERE citations.reply = replies.id AND replies.deleted = 0) OR NOT EXISTS (SELECT 1 FROM topics WHERE citations.topic = topics.id AND topics.deleted = 0))', $_SESSION['UID']);
@@ -87,7 +90,7 @@
echo '</form>';
}
if(!($new_watchlists && $new_citations)) {
if(!($new_watchlists || $new_citations)) {
echo "<p>Nothing to see here...</p>";
}
View
@@ -0,0 +1,41 @@
self.addEventListener('push', function(event) {
console.log('Received a push message', event);
var title = 'Minichan Notification';
var body = 'Something on Minichan requires your attention.';
var icon = '/favicon.gif';
var tag = 'minichan';
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag
})
);
});
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event.notification.tag);
// Android doesn't close the notification when you click on it
// See: http://crbug.com/463146
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(
clients.matchAll({
type: "window"
})
.then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/notifications' && 'focus' in client)
return client.focus();
}
if (clients.openWindow) {
return clients.openWindow('/notifications');
}
})
);
});
Oops, something went wrong.

0 comments on commit 5d8db3b

Please sign in to comment.