Skip to content

Commit

Permalink
✨ FCM
Browse files Browse the repository at this point in the history
  • Loading branch information
xkrfer committed Apr 16, 2022
1 parent d03f660 commit d0fc84c
Show file tree
Hide file tree
Showing 6 changed files with 847 additions and 41 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"axios": "^0.26.1",
"dayjs": "^1.11.0",
"element-plus": "^2.1.7",
"firebase": "^9.6.11",
"md5": "^2.3.0",
"mitt": "^3.0.0",
"pinia": "^2.0.12",
Expand Down
3 changes: 2 additions & 1 deletion src/edge/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {ping, polling} from "@/helpers/polling";
import {State} from "@/helpers/state";
import {adapter} from "@/helpers/adapter";
import {CONNECT_NAME} from "@/helpers/constants";
import {initPush} from "@/edge/background/sw-push";

const CHROME_ID = chrome.runtime.id
let LOCKED = true
Expand Down Expand Up @@ -74,11 +75,11 @@ chrome.runtime.onConnect.addListener(function (port) {
}
});


(async () => {
const token = await State.getInstance().getToken()
if (!token) {
await State.getInstance().setLoginBadge()
}
await polling()
await initPush()
})()
28 changes: 28 additions & 0 deletions src/edge/background/sw-push.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {Utils} from "@/helpers/utils";

const PublicKey = 'BOf8bHJBzeGjYw4Yk2t3dsnxHK8-Mxo4kf3fbFf-yslmHXKGT7dlmy2m5FCKj6NecvOfaKAbZS27I6NLQHiDfvY';

export function initPush() {
addPushEvent()
subscribe()
}

function subscribe() {
const applicationServerKey = Utils.urlB64ToUint8Array(PublicKey);
registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey
})
.then(function (subscription) {
console.log(JSON.stringify(subscription))
})
.catch(function (err) {
console.log('Failed to subscribe the user: ', err);
});
}

function addPushEvent() {
self.addEventListener('push', (event: any) => {
console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);
})
}
1 change: 1 addition & 0 deletions src/edge/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare const registration: ServiceWorkerRegistration;
14 changes: 14 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,19 @@ import dayjs from "dayjs";
export const Utils = {
format(date: string | number | Date | dayjs.Dayjs | null | undefined = new Date(), formatStr: string = "YYYY/MM/DD"): string {
return dayjs(date).format(formatStr)
},
urlB64ToUint8Array(base64String: string) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');

const rawData = self.atob(base64);
const outputArray = new Uint8Array(rawData.length);

for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
}
Loading

0 comments on commit d0fc84c

Please sign in to comment.