-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): manage notification in settings screen (#14)
- Loading branch information
Showing
12 changed files
with
211 additions
and
176 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
.box { | ||
background-color: #c4c4c461; | ||
padding: 1em; | ||
margin-bottom: 1em; | ||
} | ||
|
||
.box > legend { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { API_BASE_URL } from '../constants' | ||
|
||
function urlBase64ToUint8Array(base64String: string) { | ||
const padding = '='.repeat((4 - (base64String.length % 4)) % 4) | ||
const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/') | ||
const rawData = window.atob(base64) | ||
return Uint8Array.from(Array.from(rawData).map(char => char.charCodeAt(0))) | ||
} | ||
|
||
export const isNotificationSupported = () => 'Notification' in window | ||
export const isNotificationGranted = () => isNotificationSupported() && Notification.permission == 'granted' | ||
|
||
export const unSubscribePush = async (registration: ServiceWorkerRegistration) => { | ||
let subscription = await registration.pushManager.getSubscription() | ||
if (subscription) { | ||
const ok = await subscription.unsubscribe() | ||
if (!ok) { | ||
throw new Error('Unable to renew push manager subscription.') | ||
} | ||
return | ||
} | ||
} | ||
|
||
export const subscribePush = async (registration: ServiceWorkerRegistration) => { | ||
let applicationServerKey: Uint8Array | undefined | ||
try { | ||
let subscription = await registration.pushManager.getSubscription() | ||
if (!subscription) { | ||
// No subscription: creat a new one | ||
const res = await fetch(API_BASE_URL) | ||
const data = await res.json() | ||
applicationServerKey = urlBase64ToUint8Array(data.vapid) | ||
subscription = await registration.pushManager.subscribe({ | ||
userVisibleOnly: true, | ||
applicationServerKey | ||
}) | ||
} | ||
console.log('Subscribed to push manager:', subscription.endpoint) | ||
return subscription | ||
} catch (err) { | ||
console.error('Error when creating push subscription:', err) | ||
throw err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.