Skip to content

Commit

Permalink
Merge pull request #2 from bluzphp/develop
Browse files Browse the repository at this point in the history
Updated JS library
  • Loading branch information
Anton authored Nov 12, 2018
2 parents 7aa34e7 + ebd8b8a commit 3081c9f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 33 deletions.
16 changes: 16 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
checks:
javascript: true
php:
psr2_switch_declaration: true
psr2_class_declaration: true
no_short_open_tag: true
deprecated_code_usage: true
code_rating: true
filter:
excluded_paths:
- 'tests/*'
tools:
php_analyzer: true
php_code_sniffer:
config:
standard: PSR2
2 changes: 1 addition & 1 deletion application/models/Push/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function sendPush(Row $push, $message)
json_encode([
'icon' => Router::getBaseUrl() . 'img/icon-512x512.png',
'badge' => Router::getBaseUrl() . 'img/icon-128x128.png',
'body' => $message,
'body' => $message
]),
true
);
Expand Down
6 changes: 4 additions & 2 deletions application/modules/push/views/grid.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ $uid = $grid->getUid();
require(['jquery', 'bluz.push', 'bluz.grid'], function($, push) {
$(function() {
let $subscribe = $('#subscribe');
if (push) {
if (push.isAvailable()) {
push.register();

$subscribe.on('click', function() {
push.subscribe();
return false;
Expand All @@ -26,7 +28,7 @@ $uid = $grid->getUid();

<nav class="navbar navbar-light bg-light justify-content-between">
<div>
<button id="subscribe" class="btn btn-primary"><?= __('Subscribe') ?></button>
<button id="subscribe" class="btn btn-primary"><i class="fa fa-bell fa-fw"></i> <?= __('Subscribe') ?></button>
<!-- Place filters here -->
</div>
<!-- Place search form here -->
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"license": "MIT",
"description": "bluz web push module",
"require": {
"bluzphp/framework": "~7.10",
"bluzphp/composer-plugin": "~2.1",
"minishlink/web-push": "^4.0"
}
Expand Down
69 changes: 40 additions & 29 deletions public/js/bluz.push.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,55 @@ define(['jquery', 'bluz', 'bluz.notify'], function ($, bluz, notify) {

const serverKey = 'BIMtOOXkIBAENtP9DwQXr2OAvWMkGowrjHT8GZVEPWnN_kviXX7jqZqlkd7BpPK00112zPvUXnuYNUSwcN5HuqI';

if (!('serviceWorker' in navigator)) {
console.warn('Service workers are not supported by this browser');
return false;
}
let push = {};

if (!('PushManager' in window)) {
console.warn('Push notifications are not supported by this browser');
return false;
}
/**
* Check browser compatibility
* @return {boolean}
*/
push.isAvailable = function() {
if (!('serviceWorker' in navigator)) {
console.warn('Service workers are not supported by this browser');
return false;
}

if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
console.warn('Notifications are not supported by this browser');
return false;
}
if (!('PushManager' in window)) {
console.warn('Push notifications are not supported by this browser');
return false;
}

// Try to register worker
navigator.serviceWorker.register('serviceWorker.js')
.then(() => {
console.log('[PUSH] Service worker has been registered');
// push.update();
}, e => {
console.error('[PUSH] Service worker registration failed', e);
});
if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
console.warn('Notifications are not supported by this browser');
return false;
}

let push = {};
return true;
};

/**
* Try to register Service Worker
* Please, keep worker inside root directory!
*/
push.register = function() {
// Try to register worker
navigator.serviceWorker.register('serviceWorker.js')
.then(() => {
console.log('[PUSH] Service worker has been registered');
// push.update();
}, e => {
console.error('[PUSH] Service worker registration failed', e);
});
};

push.subscribe = function() {
console.log('push.unsubscribe');
console.log('[PUSH] Subscribe');
navigator.serviceWorker.ready
.then(serviceWorkerRegistration => {
console.log(serviceWorkerRegistration);
return serviceWorkerRegistration.pushManager.subscribe({
.then(serviceWorkerRegistration => serviceWorkerRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(serverKey),
})
})
)
.then(subscription => {
console.log(subscription);
// Subscription was successful
// create subscription on your server
return sendSubscriptionToServer(subscription, 'POST');
Expand All @@ -68,7 +79,7 @@ define(['jquery', 'bluz', 'bluz.notify'], function ($, bluz, notify) {
};

push.update = function() {
console.log('push.update');
console.log('[PUSH] Update subscription');
navigator.serviceWorker.ready
.then(serviceWorkerRegistration => serviceWorkerRegistration.pushManager.getSubscription())
.then(subscription => {
Expand All @@ -85,7 +96,7 @@ define(['jquery', 'bluz', 'bluz.notify'], function ($, bluz, notify) {
};

push.unsubscribe = function() {
console.log('push.unsubscribe');
console.log('[PUSH] Unsubscribe');
// To unsubscribe from push messaging, you need to get the subscription object
navigator.serviceWorker.ready
.then(serviceWorkerRegistration => serviceWorkerRegistration.pushManager.getSubscription())
Expand Down
2 changes: 1 addition & 1 deletion public/serviceWorker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Service Worker
*
* Please keep it inside root directory!
* Please, keep it inside root directory!
*
* @link https://github.com/web-push-libs/web-push-php
*/
Expand Down

0 comments on commit 3081c9f

Please sign in to comment.