Skip to content

Commit 4981e8e

Browse files
committed
Added support for VAPID
1 parent fdd89b6 commit 4981e8e

File tree

14 files changed

+1472
-285
lines changed

14 files changed

+1472
-285
lines changed

.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ MAIL_USERNAME=null
2727
MAIL_PASSWORD=null
2828
MAIL_ENCRYPTION=null
2929

30-
GCM_KEY=
31-
GCM_SENDER_ID=
32-
3330
BROADCAST_DRIVER="pusher"
3431

3532
PUSHER_APP_ID=
3633
PUSHER_APP_KEY=
3734
PUSHER_APP_SECRET=
3835
PUSHER_CLUSTER=us
36+
37+
VAPID_PUBLIC_KEY=
38+
VAPID_PRIVATE_KEY=

.eslintrc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
{
22
"extends": "vue",
3-
"parserOptions": {
4-
"ecmaVersion": 8
5-
},
63
"env": {
7-
"es6": true,
8-
"node": true,
94
"browser": true
105
}
116
}

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.0.0 - 2017-03-25
4+
5+
- Added support for VAPID.
6+
See [laravel-notification-channels/webpush@1.0.0](https://github.com/laravel-notification-channels/webpush/releases/tag/1.0.0)
7+
38
## 0.3.0 - 2017-01-26
49

510
- Upgraded to Laravel 5.4.

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,16 @@
1313
- `php artisan key:generate`
1414
- Edit `.env`
1515
- Set your database connection details
16-
- Set `GCM_KEY` and `GCM_SENDER_ID` from [Google Console](https://console.cloud.google.com)
16+
- (optional) Set `GCM_KEY` and `GCM_SENDER_ID` from [Google Console](https://console.cloud.google.com)
1717
- (optional) Set `PUSHER_APP_ID`, `PUSHER_APP_KEY`, `PUSHER_APP_SECRET` from [Pusher](https://pusher.com/)
1818
- `php artisan migrate`
19-
- (optional) `npm install` / `yarn` && `npm run dev`
19+
- `php artisan webpush:vapid` - Generates the VAPID keys required for browser authentication.
20+
- (optional) `npm install` or `yarn` and `npm run dev`
2021

2122
## Browser Compatibility
2223

2324
The [Push API](https://developer.mozilla.org/en/docs/Web/API/Push_API) currently works on Chrome and Firefox. Some features like the notification close event only works on Chrome.
2425

25-
## Known Issues
26-
27-
- If you use [Laravel Valet](https://github.com/laravel/valet) for your local development the service worker might not start because of the self signed certificate. At least that happened to me on Windows, so I had to use Apache.
28-
2926
## Changelog
3027

3128
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"type": "project",
77
"require": {
88
"php": ">=5.6.4",
9+
"laravel-notification-channels/webpush": "^1.0",
910
"laravel/framework": "5.4.*",
1011
"laravel/tinker": "~1.0",
11-
"laravel-notification-channels/webpush": "^0.2",
1212
"pusher/pusher-php-server": "^2.6"
1313
},
1414
"require-dev": {

config/services.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,4 @@
3535
'secret' => env('STRIPE_SECRET'),
3636
],
3737

38-
'gcm' => [
39-
'key' => env('GCM_KEY'),
40-
'sender_id' => env('GCM_SENDER_ID'),
41-
],
4238
];

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
{
22
"private": true,
33
"scripts": {
4-
"dev": "node node_modules/cross-env/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
5-
"watch": "node node_modules/cross-env/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
6-
"hot": "node node_modules/cross-env/bin/cross-env.js NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
7-
"production": "node node_modules/cross-env/bin/cross-env.js NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
4+
"dev": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
5+
"watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
6+
"watch-poll": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --watch-poll --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
7+
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
8+
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
89
},
910
"dependencies": {
1011
"axios": "^0.15.3",
1112
"bootstrap-sass": "^3.3.7",
1213
"jquery": "^3.1.0",
1314
"laravel-echo": "^1.2.0",
1415
"pusher-js": "^4.0.0",
15-
"vue": "^2.1.8",
16+
"vue": "^2.2.4",
1617
"vue-timeago": "^3.1.5"
1718
},
1819
"devDependencies": {
@@ -21,6 +22,6 @@
2122
"eslint-plugin-html": "^1.7.0",
2223
"eslint-plugin-vue": "^1.0.0",
2324
"json-loader": "^0.5.4",
24-
"laravel-mix": "^0.8.8"
25+
"laravel-mix": "^0.9.2"
2526
}
2627
}

public/js/app.js

Lines changed: 7 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/assets/js/bootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ window.jQuery = window.$ = $
1414
require('bootstrap-sass/assets/javascripts/bootstrap')
1515

1616
// Configure Laravel Echo
17-
const { key, cluster } = window.PUSHER_OPTIONS
17+
const { key, cluster } = window.Laravel.pusher
1818
if (key) {
1919
window.Echo = new Echo({
2020
broadcaster: 'pusher',

resources/assets/js/components/NotificationsDemo.vue

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ export default {
8686
*/
8787
subscribe () {
8888
navigator.serviceWorker.ready.then(registration => {
89-
registration.pushManager.subscribe({ userVisibleOnly: true })
89+
const options = { userVisibleOnly: true }
90+
const vapidPublicKey = window.Laravel.vapidPublicKey
91+
92+
if (vapidPublicKey) {
93+
options.applicationServerKey = this.urlBase64ToUint8Array(vapidPublicKey)
94+
}
95+
96+
registration.pushManager.subscribe(options)
9097
.then(subscription => {
9198
this.isPushEnabled = true
9299
this.pushButtonDisabled = false
@@ -185,6 +192,28 @@ export default {
185192
axios.post('/notifications')
186193
.catch(error => console.log(error))
187194
.then(() => { this.loading = false })
195+
},
196+
197+
/**
198+
* https://github.com/Minishlink/physbook/blob/02a0d5d7ca0d5d2cc6d308a3a9b81244c63b3f14/app/Resources/public/js/app.js#L177
199+
*
200+
* @param {String} base64String
201+
* @return {Uint8Array}
202+
*/
203+
urlBase64ToUint8Array (base64String) {
204+
const padding = '='.repeat((4 - base64String.length % 4) % 4);
205+
const base64 = (base64String + padding)
206+
.replace(/\-/g, '+')
207+
.replace(/_/g, '/')
208+
209+
const rawData = window.atob(base64)
210+
const outputArray = new Uint8Array(rawData.length)
211+
212+
for (let i = 0; i < rawData.length; ++i) {
213+
outputArray[i] = rawData.charCodeAt(i)
214+
}
215+
216+
return outputArray
188217
}
189218
}
190219
}

0 commit comments

Comments
 (0)