Merge branch 'sw-always-register' into swn

This commit is contained in:
tamaina 2022-03-15 22:22:10 +09:00
commit 6e19ca8b22
2 changed files with 15 additions and 16 deletions

View File

@ -356,7 +356,7 @@ antennaExcludeKeywords: "除外キーワード"
antennaKeywordsDescription: "スペースで区切るとAND指定になり、改行で区切るとOR指定になります" antennaKeywordsDescription: "スペースで区切るとAND指定になり、改行で区切るとOR指定になります"
notifyAntenna: "新しいノートを通知する" notifyAntenna: "新しいノートを通知する"
withFileAntenna: "ファイルが添付されたノートのみ" withFileAntenna: "ファイルが添付されたノートのみ"
enableServiceworker: "ServiceWorkerを有効にする" enableServiceworker: "ブラウザへのプッシュ通知を有効にする"
antennaUsersDescription: "ユーザー名を改行で区切って指定します" antennaUsersDescription: "ユーザー名を改行で区切って指定します"
caseSensitive: "大文字小文字を区別する" caseSensitive: "大文字小文字を区別する"
withReplies: "返信を含む" withReplies: "返信を含む"

View File

@ -4,23 +4,22 @@ import { api } from '@/os';
import { lang, version } from '@/config'; import { lang, version } from '@/config';
export async function initializeSw() { export async function initializeSw() {
if (instance.swPublickey && if (!('serviceWorker' in navigator)) return;
('serviceWorker' in navigator) &&
('PushManager' in window) &&
$i && $i.token) {
navigator.serviceWorker.register(`/sw.${version}.js`, { scope: '/', type: 'classic' });
navigator.serviceWorker.register(`/sw.${version}.js`, { scope: '/', type: 'classic' });
navigator.serviceWorker.ready.then(registration => { navigator.serviceWorker.ready.then(registration => {
registration.active?.postMessage({ registration.active?.postMessage({
msg: 'initialize', msg: 'initialize',
lang, lang,
}); });
if (instance.swPublickey && ('PushManager' in window) && $i && $i.token) {
// SEE: https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe#Parameters // SEE: https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe#Parameters
registration.pushManager.subscribe({ registration.pushManager.subscribe({
userVisibleOnly: true, userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(instance.swPublickey) applicationServerKey: urlBase64ToUint8Array(instance.swPublickey)
}).then(subscription => { })
.then(subscription => {
function encode(buffer: ArrayBuffer | null) { function encode(buffer: ArrayBuffer | null) {
return btoa(String.fromCharCode.apply(null, new Uint8Array(buffer))); return btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)));
} }
@ -45,8 +44,8 @@ export async function initializeSw() {
const subscription = await registration.pushManager.getSubscription(); const subscription = await registration.pushManager.getSubscription();
if (subscription) subscription.unsubscribe(); if (subscription) subscription.unsubscribe();
}); });
});
} }
});
} }
/** /**