Skip to content

Commit

Permalink
Upgrade web-push-php to 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Minishlink committed Mar 17, 2018
1 parent 0694415 commit 53f05d3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
}
],
"require": {
"minishlink/web-push": "^3.0"
"minishlink/web-push": "^4.0"
}
}
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,15 @@ document.addEventListener("DOMContentLoaded", () => {
function push_sendSubscriptionToServer(subscription, method) {
const key = subscription.getKey('p256dh');
const token = subscription.getKey('auth');
const contentEncoding = (PushManager.supportedContentEncodings || ['aesgcm'])[0];

return fetch('push_subscription.php', {
method,
body: JSON.stringify({
endpoint: subscription.endpoint,
key: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : null,
token: token ? btoa(String.fromCharCode.apply(null, new Uint8Array(token))) : null
publicKey: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : null,
authToken: token ? btoa(String.fromCharCode.apply(null, new Uint8Array(token))) : null,
contentEncoding,
}),
}).then(() => subscription);
}
Expand All @@ -208,13 +210,15 @@ document.addEventListener("DOMContentLoaded", () => {

const key = subscription.getKey('p256dh');
const token = subscription.getKey('auth');
const contentEncoding = (PushManager.supportedContentEncodings || ['aesgcm'])[0];

fetch('send_push_notification.php', {
method: 'POST',
body: JSON.stringify({
endpoint: subscription.endpoint,
key: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('p256dh')))) : null,
token: token ? btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('auth')))) : null
publicKey: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('p256dh')))) : null,
authToken: token ? btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('auth')))) : null,
contentEncoding,
})
})
})
Expand Down
7 changes: 3 additions & 4 deletions src/send_push_notification.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription;

// here I'll get the subscription endpoint in the POST parameters
// but in reality, you'll get this information in your database
// because you already stored it (cf. push_subscription.php)
$subscription = json_decode(file_get_contents('php://input'), true);
$subscription = Subscription::create(json_decode(file_get_contents('php://input'), true));

$auth = array(
'VAPID' => array(
Expand All @@ -18,10 +19,8 @@
$webPush = new WebPush($auth);

$res = $webPush->sendNotification(
$subscription['endpoint'],
$subscription,
"Hello!",
$subscription['key'],
$subscription['token'],
true
);

Expand Down

0 comments on commit 53f05d3

Please sign in to comment.