fix(push): preserve Android status after LiveView patches
This commit is contained in:
parent
cba544ffda
commit
8e4656a725
|
|
@ -660,14 +660,18 @@ export const Hooks = {
|
|||
this.buttonLabel = this.el.querySelector("[data-enable-push-label]")
|
||||
this.status = this.el.querySelector("[data-push-status]")
|
||||
this.native = typeof window.WhoNeedHelpAndroid?.postMessage === "function"
|
||||
this.statusMessage = null
|
||||
this.buttonEnabled = null
|
||||
|
||||
if (this.native && this.button) this.button.disabled = false
|
||||
|
||||
this.setStatus = message => {
|
||||
this.statusMessage = message
|
||||
if (this.status) this.status.textContent = message
|
||||
}
|
||||
|
||||
this.setButtonState = enabled => {
|
||||
this.buttonEnabled = enabled
|
||||
if (!this.buttonLabel) return
|
||||
this.buttonLabel.textContent = enabled
|
||||
? this.buttonLabel.dataset.enabledLabel
|
||||
|
|
@ -765,9 +769,11 @@ export const Hooks = {
|
|||
device_label: String(detail.device_label || "Android · FCM").slice(0, 120),
|
||||
user_agent: navigator.userAgent.slice(0, 500)
|
||||
})
|
||||
this.setButtonState(true)
|
||||
this.setStatus("Push notifications are enabled on this Android device.")
|
||||
} catch (error) {
|
||||
console.warn("Android push registration failed", error)
|
||||
this.setButtonState(false)
|
||||
this.setStatus("The Android push token could not be saved. Please try again.")
|
||||
} finally {
|
||||
if (this.button) this.button.disabled = false
|
||||
|
|
@ -776,6 +782,7 @@ export const Hooks = {
|
|||
|
||||
this.nativeError = event => {
|
||||
const reason = event.detail?.reason
|
||||
this.setButtonState(false)
|
||||
this.setStatus(
|
||||
reason === "permission_denied"
|
||||
? "Notification permission was not granted."
|
||||
|
|
@ -794,6 +801,8 @@ export const Hooks = {
|
|||
window.localStorage.removeItem("wnh.push.server-device-id")
|
||||
if (this.native) {
|
||||
this.postNative({action: "disable_push"})
|
||||
this.setButtonState(false)
|
||||
this.setStatus("Push notifications are disabled on this device.")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -857,6 +866,23 @@ export const Hooks = {
|
|||
}
|
||||
}
|
||||
|
||||
this.activeServerDevice = () => {
|
||||
const deviceId = window.localStorage.getItem("wnh.push.server-device-id")
|
||||
|
||||
return Boolean(
|
||||
deviceId &&
|
||||
[...this.el.querySelectorAll("[data-disable-device]")]
|
||||
.some(element => element.dataset.disableDevice === deviceId)
|
||||
)
|
||||
}
|
||||
|
||||
this.restoreNativeState = () => {
|
||||
if (!this.native || !this.activeServerDevice()) return
|
||||
|
||||
this.setButtonState(true)
|
||||
this.setStatus("Push notifications are enabled on this Android device.")
|
||||
}
|
||||
|
||||
this.restoreWebState = async () => {
|
||||
if (this.native) return
|
||||
|
||||
|
|
@ -901,6 +927,7 @@ export const Hooks = {
|
|||
this.el.addEventListener("click", this.disableCurrentDevice)
|
||||
this.button?.addEventListener("click", this.enable)
|
||||
if (this.native) {
|
||||
this.restoreNativeState()
|
||||
this.postNative({action: "push_token_request"})
|
||||
} else {
|
||||
this.restoreWebState()
|
||||
|
|
@ -923,7 +950,11 @@ export const Hooks = {
|
|||
this.button?.removeEventListener("click", this.enable)
|
||||
this.button?.addEventListener("click", this.enable)
|
||||
|
||||
if (this.buttonEnabled !== null) this.setButtonState(this.buttonEnabled)
|
||||
if (this.statusMessage !== null) this.setStatus(this.statusMessage)
|
||||
|
||||
if (this.native) {
|
||||
this.restoreNativeState()
|
||||
this.postNative({action: "push_token_request"})
|
||||
} else {
|
||||
this.restoreWebState()
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user