Skip to content

Commit

Permalink
update push function
Browse files Browse the repository at this point in the history
  • Loading branch information
appsaeed committed Mar 20, 2024
1 parent e507e1e commit 0e3d6d6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/MainProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useDispatch } from "react-redux";
import { ToastContainer } from "react-toastify";
import 'react-toastify/dist/ReactToastify.css';
import settings from "./app/settings";
import { subscribe } from "./app/utiles";
import { pushSubscribe } from "./app/utiles";
import useNotify from "./hooks/useNotify";
import { notifyTokenUpdate } from "./redux/notifyStore";

Expand All @@ -14,7 +14,7 @@ export default function MainProvider({ children }: { children: ReactNode }) {
useEffect(() => {
const worker_path = settings.url + '/push.js';
if (notify.token) {
subscribe(worker_path)
pushSubscribe(worker_path)
.then((token) => dispatch(notifyTokenUpdate(token)))
.catch((err) => console.log("ERROR:: ", err));
}
Expand Down
30 changes: 1 addition & 29 deletions src/app/utiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const grammarlyItem = (count: number, items: [string, string, string | un
}


export function subscribe(worker_path: string): Promise<string> {
export function pushSubscribe(worker_path: string): Promise<string> {
return new Promise((resolve, reject) => {
try {

Expand Down Expand Up @@ -107,31 +107,3 @@ export function subscribe(worker_path: string): Promise<string> {
}
})
}


export async function pushSubscribe(callback: (token: string) => void) {

const permission = await Notification.requestPermission();
if (permission !== 'granted') {
throw ('Your notification is not allowed please check permissions')
}

if (!("serviceWorker" in navigator)) {
throw ('Your notification is not allowed please check permissions');
}

const worker_path = settings.url + '/push.js';
const register = await navigator.serviceWorker.register(worker_path);

const subscription = await register.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(import.meta.env.VITE_NOTIFY_PUBLIC_KEY)
})

const token = btoa(JSON.stringify(subscription));

// localStorage.setItem('notification_token', token)
return callback(token);
}


4 changes: 2 additions & 2 deletions src/pages/Tokenization.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";
import { useDispatch } from "react-redux";
import { toast } from "react-toastify";
import { errorToString, subscribe, worker_path } from "../app/utiles";
import { errorToString, pushSubscribe, worker_path } from "../app/utiles";
import useNotify from "../hooks/useNotify";
import { notifyTokenUpdate } from "../redux/notifyStore";

Expand All @@ -16,7 +16,7 @@ export default function Tokenization() {

setLoading(true)

subscribe(worker_path)
pushSubscribe(worker_path)
.then(token => dispatch(notifyTokenUpdate(token)))
.catch(error => toast.error(errorToString(error)))
.finally(() => setLoading(false));
Expand Down

0 comments on commit 0e3d6d6

Please sign in to comment.