Skip to content

Instantly share code, notes, and snippets.

View Serginho's full-sized avatar
🏠
Working from home

Serginho Serginho

🏠
Working from home
View GitHub Profile
@Serginho
Serginho / 1global.ts
Last active March 19, 2019 20:18
Environment splitted configuration
// global.ts
export const ENVIRONMENT_GLOBAL = {
appVersion: '1.0',
browserSupport: [
{
name: 'Chrome',
version: 45
},
{
name: 'Firefox',
@Serginho
Serginho / 1environment.ts
Last active March 19, 2019 20:18
Environment base configuration
// environment.ts configuration
export const environment = {
production: false,
appVersion: '1.0'
endpoint: 'mysite.dev/rest',
browserSupport: [
{
name: 'Chrome',
version: 45
},
@Serginho
Serginho / push-upload-browser.ts
Last active March 18, 2019 19:41
request subscription browser
navigator.serviceWorker.ready.then(serviceWorkerRegistration => {
const options = {userVisibleOnly: true, applicationServerKey: applicationServerKey};
serviceWorkerRegistration.pushManager.subscribe(options).then(pushSubscription => {
// Send subscription to service worker to save it.
navigator.serviceWorker.controller.postMessage({
action: 'REQUEST_SUBSCRIPTION',
subscription: pushSubscription
});
// Upload first subscription to server using userId or any identifier
// to match user.
@Serginho
Serginho / push-upload-browser.ts
Created March 18, 2019 19:39
request subscription browser
navigator.serviceWorker.ready.then(serviceWorkerRegistration => {
const options = {userVisibleOnly: true, applicationServerKey: applicationServerKey};
serviceWorkerRegistration.pushManager.subscribe(options).then(pushSubscription => {
navigator.serviceWorker.controller.postMessage({
action: 'REQUEST_SUBSCRIPTION',
subscription: pushSubscription
});
uploadSubscriptionToTheServer(pushSubscription, userId);
});
});
@Serginho
Serginho / solution-1-upload-token-worker.ts
Last active March 18, 2019 19:29
push subscriptionchange upload token null
// To use IndexedDB like localStorage use this library.
// https://github.com/RyotaSugawara/serviceworker-storage
const storage = new ServiceWorkerStorage('sw:storage', 1);
async handlePush() {
const newSubscription = await self.registration.pushManager.getSubscription();
const oldSubscription = JSON.parse(await storage.getItem('subscription'));
if(newSubscription.endpoint !== oldSubscription.endpoint) {
storage.setItem('subscription', newSubscription);
const req = new Request('/refreshpushsubscription', {
@Serginho
Serginho / problem-1-upload-token.ts
Last active March 18, 2019 18:36
push subscriptionchange upload token null
async handlePush(event: PushSubscriptionChangeEvent) {
const req = new Request('/refreshpushsubscription', {
method: 'POST',
body: JSON.stringify(
{oldSubscription: event.oldSubscription, newSubscription: event.newSubscription})
});
const response = await self.fetch(req);
}
self.addEventListener('pushsubscriptionchange', (event: PushSubscriptionChangeEvent) =>
@Serginho
Serginho / main.ts
Last active March 14, 2019 19:02
push subscription change implementation
self.addEventListener('pushsubscriptionchange', (event: PushSubscriptionChangeEvent) => {
//Renew your subscription here.
});
@Serginho
Serginho / pushsubscriptionchange.d.ts
Last active March 14, 2019 18:57
PushSubscriptionChangeEvent definition
interface PushSubscriptionChangeEvent extends ExtendableEvent {
readonly newSubscription?: PushSubscription;
readonly oldSubscription?: PushSubscription;
}