Page MenuHomePhabricator

D12905.diff
No OneTemporary

D12905.diff

diff --git a/web/cashtab/src/components/App.js b/web/cashtab/src/components/App.js
--- a/web/cashtab/src/components/App.js
+++ b/web/cashtab/src/components/App.js
@@ -393,6 +393,35 @@
const selectedKey =
location && location.pathname ? location.pathname.substr(1) : '';
+ /*
+ See https://web.dev/push-notifications-subscribing-a-user/
+ note that Safari still uses callback syntax, hence this method which handles both syntaxes
+ https://developer.mozilla.org/en-US/docs/Web/API/Notification/requestPermission
+ */
+
+ const askPermissionForNotifications = () => {
+ return new Promise(function (resolve, reject) {
+ const permissionResult = Notification.requestPermission(function (
+ result,
+ ) {
+ resolve(result);
+ });
+
+ if (permissionResult) {
+ permissionResult.then(resolve, reject);
+ }
+ }).then(function (permissionResult) {
+ console.log(`permissionResult`, permissionResult);
+ if (permissionResult !== 'granted') {
+ throw new Error("We weren't granted permission.");
+ // TODO in this case, we should display a modal saying we'll keep asking
+ } else {
+ // If it is granted, ask again for persistent storage
+ // Best way to do this is to show a modal that refreshes the page
+ }
+ });
+ };
+
const checkForPersistentStorage = async () => {
// Request persistent storage for site
if (navigator.storage && navigator.storage.persist) {
@@ -407,6 +436,36 @@
console.log(
`Persistent storage granted: ${persistanceRequestResult}`,
);
+ // If the request was not granted, ask the user to approve notification permissions
+ if (!persistanceRequestResult) {
+ // Check if the browser supports notifications
+ const browserSupportsNotifications =
+ 'Notification' in window;
+
+ console.log(
+ `browserSupportsNotifications`,
+ browserSupportsNotifications,
+ );
+
+ // If the browser supports notifications, check the permission status
+ if (browserSupportsNotifications) {
+ const cashtabNotificationPermission =
+ Notification.permission;
+
+ console.log(
+ `cashtabNotificationPermission`,
+ cashtabNotificationPermission,
+ );
+
+ // If the permission is not 'granted', ask for notification permissions
+ if (cashtabNotificationPermission !== 'granted') {
+ console.log(
+ `User has not granted notification permission and persistent storage status has not been granted. Requesting notification permission.`,
+ );
+ return askPermissionForNotifications();
+ }
+ }
+ }
}
}
};

File Metadata

Mime Type
text/plain
Expires
Tue, May 20, 23:32 (17 m, 24 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5866152
Default Alt Text
D12905.diff (3 KB)

Event Timeline