-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2735 from daostack/dev
Sprint 40
- Loading branch information
Showing
18 changed files
with
374 additions
and
45 deletions.
There are no files selected for viewing
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,82 @@ | ||
/* eslint-disable */ | ||
// firebase-messaging-sw.js | ||
importScripts( | ||
"https://www.gstatic.com/firebasejs/10.7.1/firebase-app-compat.js", | ||
); | ||
importScripts( | ||
"https://www.gstatic.com/firebasejs/10.7.1/firebase-messaging-compat.js", | ||
); | ||
|
||
const ENV = { | ||
LOCAL: "http://localhost:3000", | ||
DEV: "https://web-dev.common.io", | ||
STAGE: "https://web-staging.common.io", | ||
PRODUCTION: "https://common.io", | ||
}; | ||
|
||
const FIREBASE_CONFIG_ENV = { | ||
DEV: { | ||
apiKey: "AIzaSyDbTFuksgOkIVWDiFe_HG7-BE8X6Dwsg-0", | ||
authDomain: "common-dev-34b09.firebaseapp.com", | ||
databaseURL: "https://common-dev-34b09.firebaseio.com", | ||
projectId: "common-dev-34b09", | ||
storageBucket: "common-dev-34b09.appspot.com", | ||
messagingSenderId: "870639147922", | ||
appId: "1:870639147922:web:9ee954bb1dd52e25cb7f4b", | ||
}, | ||
STAGE: { | ||
apiKey: "AIzaSyBASCWJMV64mZJObeFEitLmdUC1HqmtjJk", | ||
authDomain: "common-staging-1d426.firebaseapp.com", | ||
databaseURL: "https://common-staging-1d426.firebaseio.com", | ||
projectId: "common-staging-1d426", | ||
storageBucket: "common-staging-1d426.appspot.com", | ||
messagingSenderId: "701579202562", | ||
appId: "1:701579202562:web:5729d8a875f98f6709571b", | ||
}, | ||
PRODUCTION: { | ||
apiKey: "AIzaSyAlYrKLd6KNKVkhmNEMKfb0cWHSWicCBOY", | ||
authDomain: "common-production-67641.firebaseapp.com", | ||
databaseURL: "https://common-production-67641.firebaseio.com", | ||
projectId: "common-production-67641", | ||
storageBucket: "common-production-67641.appspot.com", | ||
messagingSenderId: "461029494046", | ||
appId: "1:461029494046:web:4e2e4afbbeb7b487b48d0f", | ||
}, | ||
}; | ||
|
||
let firebaseConfig = {}; | ||
|
||
switch (location.origin) { | ||
case ENV.LOCAL: | ||
case ENV.DEV: { | ||
firebaseConfig = FIREBASE_CONFIG_ENV.DEV; | ||
break; | ||
} | ||
case ENV.STAGE: { | ||
firebaseConfig = FIREBASE_CONFIG_ENV.STAGE; | ||
break; | ||
} | ||
case ENV.PRODUCTION: { | ||
firebaseConfig = FIREBASE_CONFIG_ENV.PRODUCTION; | ||
break; | ||
} | ||
default: { | ||
firebaseConfig = FIREBASE_CONFIG_ENV.DEV; | ||
break; | ||
} | ||
} | ||
|
||
firebase.initializeApp(firebaseConfig); | ||
|
||
const messaging = firebase.messaging(); | ||
|
||
messaging.onBackgroundMessage((payload) => { | ||
const notificationTitle = payload.notification.title; | ||
const notificationOptions = { | ||
body: payload.notification.body, | ||
data: payload.data, | ||
icon: "/logo.png", | ||
}; | ||
|
||
self.registration.showNotification(notificationTitle, notificationOptions); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
71 changes: 71 additions & 0 deletions
71
src/pages/App/handlers/NotificationsHandler/NotificationsHandler.tsx
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,71 @@ | ||
import { FC, useEffect, useState } from "react"; | ||
import { useSelector } from "react-redux"; | ||
import { selectUser } from "@/pages/Auth/store/selectors"; | ||
import { NotificationService } from "@/services"; | ||
|
||
const NotificationsHandler: FC = () => { | ||
const user = useSelector(selectUser()); | ||
const userId = user?.uid; | ||
const [isRegistered, setIsRegistered] = useState(false); | ||
|
||
function initServiceWorker() { | ||
navigator.serviceWorker | ||
.register("/firebase-messaging-sw.js") | ||
.then((registration) => { | ||
setIsRegistered(true); | ||
return registration; | ||
}) | ||
.catch((err) => { | ||
console.log("ServiceWorker registration failed: ", err); | ||
}); | ||
} | ||
|
||
// Check if the service worker is already registered or register a new one | ||
useEffect(() => { | ||
if ("serviceWorker" in navigator) { | ||
navigator.serviceWorker | ||
.getRegistration("/firebase-messaging-sw.js") | ||
.then((existingRegistration) => { | ||
if (existingRegistration) { | ||
setIsRegistered(true); | ||
} else { | ||
initServiceWorker(); | ||
} | ||
|
||
return; | ||
}) | ||
.catch((err) => { | ||
console.log("Error checking service worker registration: ", err); | ||
}); | ||
} | ||
}, []); | ||
|
||
// Handle notification permissions and foreground message listener | ||
useEffect(() => { | ||
if (!userId || !isRegistered) { | ||
return; | ||
} | ||
|
||
let unsubscribeOnMessage; | ||
(async () => { | ||
const hasPermissions = await NotificationService.requestPermissions(); | ||
if (!hasPermissions) { | ||
console.log("Notification permissions denied"); | ||
return; | ||
} | ||
|
||
await NotificationService.saveFCMToken(); | ||
unsubscribeOnMessage = NotificationService.onForegroundMessage(); | ||
})(); | ||
|
||
return () => { | ||
if (unsubscribeOnMessage) { | ||
unsubscribeOnMessage(); | ||
} | ||
}; | ||
}, [userId, isRegistered]); | ||
|
||
return null; | ||
}; | ||
|
||
export default NotificationsHandler; |
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 @@ | ||
export { default as NotificationsHandler } from "./NotificationsHandler"; |
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,73 @@ | ||
import firebase from "@/shared/utils/firebase"; | ||
import firebaseConfig from "@/config"; | ||
import Api from "./Api"; | ||
|
||
enum NOTIFICATIONS_PERMISSIONS { | ||
DEFAULT = "default", | ||
DENIED = "denied", | ||
GRANTED = "granted" | ||
} | ||
|
||
|
||
class NotificationService { | ||
private endpoints: { | ||
setFCMToken: string; | ||
}; | ||
|
||
constructor() { | ||
this.endpoints = { | ||
setFCMToken: '/users/auth/google/set-fcm-token', | ||
}; | ||
} | ||
|
||
public requestPermissions = async (): Promise<boolean> => { | ||
try { | ||
if(Notification.permission === NOTIFICATIONS_PERMISSIONS.GRANTED) { | ||
return true; | ||
} | ||
const permission = await Notification.requestPermission(); | ||
if (permission === NOTIFICATIONS_PERMISSIONS.GRANTED) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} catch (err) { | ||
return false; | ||
} | ||
} | ||
|
||
public saveFCMToken = async (): Promise<void> => { | ||
try { | ||
const token = await firebase.messaging().getToken({ vapidKey: firebaseConfig.vapidKey }); | ||
if (token) { | ||
|
||
await Api.post( | ||
this.endpoints.setFCMToken, | ||
{ | ||
token, | ||
} | ||
); | ||
} | ||
} catch (error) { | ||
console.error("An error occurred while retrieving token. ", error); | ||
} | ||
} | ||
|
||
public onForegroundMessage = () => { | ||
const unsubscribe = firebase.messaging().onMessage((payload) => { | ||
|
||
const { title, body } = payload.notification; | ||
if (Notification.permission === 'granted') { | ||
new Notification(title, { | ||
body, | ||
data: payload?.data, | ||
icon: "/logo.png", | ||
}); | ||
} | ||
}); | ||
|
||
return unsubscribe; | ||
} | ||
} | ||
|
||
export default new NotificationService(); |
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 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 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 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
Oops, something went wrong.