fix notifs on ios
This commit is contained in:
parent
a7b349241c
commit
6a1d9299f5
@ -44,38 +44,52 @@ async function unregisterPush() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function registerPush(publicKey) {
|
async function registerPush(publicKey) {
|
||||||
checkServiceWorkerSupport();
|
checkServiceWorkerSupport();
|
||||||
|
|
||||||
const permission = await Notification.requestPermission();
|
if (window.IS_IOS && !window.IS_STANDALONE) {
|
||||||
if (permission !== "granted") {
|
throw new Error(
|
||||||
throw new Error("Notification permission not granted");
|
"On iOS, notifications require adding this site to the Home Screen."
|
||||||
}
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const registration = await navigator.serviceWorker.register("/assets/custom/serviceWorker.js");
|
const permission = await Notification.requestPermission();
|
||||||
|
if (permission !== "granted") {
|
||||||
|
throw new Error("Notification permission not granted");
|
||||||
|
}
|
||||||
|
|
||||||
let subscription = await registration.pushManager.getSubscription();
|
const registration = await navigator.serviceWorker.register(
|
||||||
if (!subscription) {
|
"/assets/custom/serviceWorker.js",
|
||||||
const applicationServerKey = urlBase64ToUint8Array(publicKey);
|
{ scope: "/" }
|
||||||
subscription = await registration.pushManager.subscribe({
|
);
|
||||||
userVisibleOnly: true,
|
|
||||||
applicationServerKey
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch(`${window.GLOBAL_ENV.API_ENDPOINT}/v9/users/@me/devices`, {
|
let subscription = await registration.pushManager.getSubscription();
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Authorization": window.AUTH_TOKEN
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
provider: "webpush",
|
|
||||||
webpush_subscription: subscription
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!subscription) {
|
||||||
const text = await response.text();
|
const applicationServerKey = urlBase64ToUint8Array(publicKey);
|
||||||
throw new Error("Failed to register: ", text)
|
|
||||||
}
|
subscription = await registration.pushManager.subscribe({
|
||||||
|
userVisibleOnly: true,
|
||||||
|
applicationServerKey
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(
|
||||||
|
`${window.GLOBAL_ENV.API_ENDPOINT}/v9/users/@me/devices`,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: window.AUTH_TOKEN
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
provider: "webpush",
|
||||||
|
subscription
|
||||||
|
})
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const text = await response.text();
|
||||||
|
throw new Error(`Failed to register push: ${text}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -39,6 +39,7 @@
|
|||||||
|
|
||||||
window.IS_IOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
|
window.IS_IOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
|
||||||
window.IS_MOBILE = window.matchMedia("(pointer: coarse)").matches;
|
window.IS_MOBILE = window.matchMedia("(pointer: coarse)").matches;
|
||||||
|
window.IS_STANDALONE = window.matchMedia("(display-mode: standalone)").matches || window.navigator.standalone === true;
|
||||||
|
|
||||||
function protocolRelative(url) {
|
function protocolRelative(url) {
|
||||||
if (!url) return "";
|
if (!url) return "";
|
||||||
|
|||||||
Reference in New Issue
Block a user