Skip to content
This repository was archived by the owner on Mar 15, 2023. It is now read-only.

Commit 8bd2a9a

Browse files
committed
Working on docs #10 [skip-ci]
1 parent abcd9df commit 8bd2a9a

File tree

7 files changed

+82
-52
lines changed

7 files changed

+82
-52
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
An extension for implementing Web Push Notifications on your website in a breeze.
1010

11+
Documentation is at [docs/README.md](docs/README.md)
12+
1113
[![Latest Stable Version](https://poser.pugx.org/machour/yii2-web-push-notifications/v/stable.png)](https://packagist.org/packages/machour/yii2-web-push-notifications)
1214
[![Total Downloads](https://poser.pugx.org/machour/yii2-web-push-notifications/downloads.png)](https://packagist.org/packages/machour/yii2-web-push-notifications)
1315
[![tests](https://github.com/machour/yii2-web-push-notifications/workflows/tests/badge.svg)](https://github.com/machour/yii2-web-push-notifications/actions?query=workflow%3Atests)

docs/01-Installation.md

Whitespace-only changes.

docs/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Web Push Notifications for Yii2
2+
3+
This module provides a complete solution to implement your own Web Push notifications in your Yii2 application.
4+
5+
You won't need to rely anymore on a service provider like TruePush, OneSignal, ..
6+
7+
Out of the box, this module allows you to:
8+
9+
* Quickly set up a Web Push subscription mechanism on your website
10+
* Migrate from your current Web Push provider and own your subscribers
11+
12+
13+
## Summary (TODO)
14+
15+
* Installation
16+
* Concepts
17+
* Basic Usage
18+
* Migrating from a Push Provider
19+
20+
## Credits
21+
22+
This module is based on the [minishlink/web-push](https://github.com/web-push-libs/web-push-php) library.
23+
24+
It began as a fork of the [web-push-php-example](https://github.com/Minishlink/web-push-php-example) repository and
25+
turned into a full solution.
26+
27+
## Resources
28+
29+
Check out these links if you want to get a better understanding of the underlying mechanisms in action for Web Push
30+
Notifications :
31+
32+
* Matthew Gaunt's [Web Push Book](https://web-push-book.gauntface.com/) - a masterpiece.
33+
* This [web-push-php-example](https://github.com/Minishlink/web-push-php-example) - a simple yet efficient demo repository
34+
that you can clone and and run locally to start your journey
35+
* Autopush's [Error codes](https://autopush.readthedocs.io/en/latest/http.html#error-codes) manual section, that documents
36+
all possible error codes when performing a push.

src/assets/web-push.js

+15-23
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ var WebPush = /** @class */ (function () {
6262
this.log("ServiceWorker registration failed: ", err);
6363
});
6464
};
65-
WebPush.prototype.checkSubscription = function (successCb, failureCb) {
65+
WebPush.prototype.checkSubscription = function (successCb, failureCb, shouldMigrate) {
6666
var _this = this;
6767
if (successCb === void 0) { successCb = function (status) { }; }
6868
if (failureCb === void 0) { failureCb = function (error) { }; }
69+
if (shouldMigrate === void 0) { shouldMigrate = function (context) { return false; }; }
6970
navigator.serviceWorker.ready
7071
.then(function (serviceWorkerRegistration) {
7172
return serviceWorkerRegistration.pushManager.getSubscription();
@@ -76,30 +77,21 @@ var WebPush = /** @class */ (function () {
7677
successCb(SubscriptionStatus.UNSUBSCRIBED);
7778
return;
7879
}
79-
/** MIGRATION
80-
const platformDetails = localStorage.getItem("platformDetails");
81-
82-
// Truepush migration: silently unsubscribe & resubscribe the user
83-
if (platformDetails) {
84-
localStorage.setItem("_platformDetails", platformDetails);
85-
localStorage.removeItem("platformDetails");
86-
87-
return subscription
88-
.unsubscribe()
89-
.then(() => navigator.serviceWorker.ready)
90-
.then(serviceWorkerRegistration =>
91-
serviceWorkerRegistration.pushManager.subscribe({
92-
userVisibleOnly: true,
93-
applicationServerKey: this.urlBase64ToUint8Array(this.publicKey)
94-
})
95-
)
96-
.then(subscription => {
97-
return this.sync(subscription, "POST");
80+
// We are subscribed, give the possibility to migrate from an old provider
81+
if (shouldMigrate(_this)) {
82+
return subscription
83+
.unsubscribe()
84+
.then(function () { return navigator.serviceWorker.ready; })
85+
.then(function (serviceWorkerRegistration) {
86+
return serviceWorkerRegistration.pushManager.subscribe({
87+
userVisibleOnly: true,
88+
applicationServerKey: _this.urlBase64ToUint8Array(_this.publicKey)
89+
});
9890
})
99-
.then(json => {
100-
this.changePushButtonState("migrated as " + json.id);
91+
.then(function (subscription) {
92+
return _this.sync(subscription, "POST");
10193
});
102-
}*/
94+
}
10395
return _this.sync(subscription, "PUT");
10496
})
10597
.then(function (subscription) { return subscription && successCb(SubscriptionStatus.SUBSCRIBED); }) // Set your UI to show they have subscribed for push messages

src/assets/web-push.ts

+15-24
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class WebPush {
6969
);
7070
}
7171

72-
checkSubscription(successCb = (status: SubscriptionStatus) => {}, failureCb = (error) => {}) {
72+
checkSubscription(successCb = (status: SubscriptionStatus) => {}, failureCb = (error) => {}, shouldMigrate = (context) => false) {
7373
navigator.serviceWorker.ready
7474
.then(serviceWorkerRegistration =>
7575
serviceWorkerRegistration.pushManager.getSubscription()
@@ -81,30 +81,21 @@ class WebPush {
8181
return;
8282
}
8383

84-
/** MIGRATION
85-
const platformDetails = localStorage.getItem("platformDetails");
86-
87-
// Truepush migration: silently unsubscribe & resubscribe the user
88-
if (platformDetails) {
89-
localStorage.setItem("_platformDetails", platformDetails);
90-
localStorage.removeItem("platformDetails");
91-
84+
// We are subscribed, give the possibility to migrate from an old provider
85+
if (shouldMigrate(this)) {
9286
return subscription
93-
.unsubscribe()
94-
.then(() => navigator.serviceWorker.ready)
95-
.then(serviceWorkerRegistration =>
96-
serviceWorkerRegistration.pushManager.subscribe({
97-
userVisibleOnly: true,
98-
applicationServerKey: this.urlBase64ToUint8Array(this.publicKey)
99-
})
100-
)
101-
.then(subscription => {
102-
return this.sync(subscription, "POST");
103-
})
104-
.then(json => {
105-
this.changePushButtonState("migrated as " + json.id);
106-
});
107-
}*/
87+
.unsubscribe()
88+
.then(() => navigator.serviceWorker.ready)
89+
.then(serviceWorkerRegistration =>
90+
serviceWorkerRegistration.pushManager.subscribe({
91+
userVisibleOnly: true,
92+
applicationServerKey: this.urlBase64ToUint8Array(this.publicKey)
93+
})
94+
)
95+
.then(subscription => {
96+
return this.sync(subscription, "POST");
97+
});
98+
}
10899

109100
return this.sync(subscription, "PUT");
110101
})

src/widgets/SubscriptionWidget.php

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class SubscriptionWidget extends Widget
1111
/** @var WpnApp */
1212
public $app;
1313

14+
/** @var string */
15+
public $shouldMigrate;
16+
1417
/**
1518
* @throws InvalidConfigException
1619
*/
@@ -30,6 +33,7 @@ public function run(): string
3033

3134
return $this->render('subscription', [
3235
'app' => $this->app,
36+
'shouldMigrate' => $this->shouldMigrate,
3337
]);
3438
}
3539
}

src/widgets/views/subscription.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@
22

33
/* @var $this \yii\web\View */
44
/* @var $app \machour\yii2\wpn\models\WpnApp */
5+
/* @var $shouldMigrate string */
6+
7+
if (!$shouldMigrate) {
8+
$shouldMigrate = 'function() { return false; }';
9+
}
510

611
$this->registerJs(<<<JS
7-
$(() => {
12+
jQuery(() => {
813
var wp = new WebPush({$app->id}, "{$app->public_key}");
9-
var wpnStatus = $('.wpn-status');
10-
$(".wpn-subscribe").on("click", function() {
14+
var wpnStatus = jQuery('.wpn-status');
15+
jQuery(".wpn-subscribe").on("click", function() {
1116
wp.subscribe(function(subscription) {
1217
wpnStatus.text("Subscribed, endpoint is" + subscription.endpoint);
1318
}, function (error) {
1419
wpnStatus.text("Got error: " + error);
1520
});
1621
});
1722
18-
$(".wpn-unsubscribe").on("click", function() {
23+
jQuery(".wpn-unsubscribe").on("click", function() {
1924
wp.unsubscribe(function () {
2025
wpnStatus.text("Unsubscribed");
2126
}, function () {
@@ -28,7 +33,7 @@
2833
}, function(error) {
2934
wpnStatus.text("error");
3035
console.error(error);
31-
});
36+
}, $shouldMigrate);
3237
3338
3439
});

0 commit comments

Comments
 (0)