Fix resubscribing when notifications are re-granted
(case: from denied to granted)
This commit is contained in:
@@ -44,9 +44,6 @@ class Notifier {
|
||||
}
|
||||
|
||||
async webPushSubscription(hasWebPushTopics) {
|
||||
if (!this.pushPossible()) {
|
||||
throw new Error("Unsupported or denied");
|
||||
}
|
||||
const pushManager = await this.pushManager();
|
||||
const existingSubscription = await pushManager.getSubscription();
|
||||
if (existingSubscription) {
|
||||
|
||||
@@ -27,7 +27,7 @@ class SubscriptionManager {
|
||||
* It is important to note that "mutedUntil" must be part of the where() query, otherwise the Dexie live query
|
||||
* will not react to it, and the Web Push topics will not be updated when the user mutes a topic.
|
||||
*/
|
||||
async webPushTopics(pushPossible = notifier.pushPossible()) {
|
||||
async webPushTopics(pushPossible) {
|
||||
if (!pushPossible) {
|
||||
return [];
|
||||
}
|
||||
@@ -120,13 +120,14 @@ class SubscriptionManager {
|
||||
);
|
||||
}
|
||||
|
||||
async updateWebPushSubscriptions(presetTopics) {
|
||||
const topics = presetTopics ?? (await this.webPushTopics());
|
||||
async updateWebPushSubscriptions(topics) {
|
||||
const hasWebPushTopics = topics.length > 0;
|
||||
const browserSubscription = await notifier.webPushSubscription(hasWebPushTopics);
|
||||
|
||||
if (!browserSubscription) {
|
||||
console.log("[SubscriptionManager] No browser subscription currently exists, so web push was never enabled. Skipping.");
|
||||
console.log(
|
||||
"[SubscriptionManager] No browser subscription currently exists, so web push was never enabled or the notification permission was removed. Skipping."
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -168,12 +168,12 @@ export const useNotificationPermissionListener = (query) => {
|
||||
* the service worker, since the service worker cannot play sounds.
|
||||
*/
|
||||
const useWebPushListener = (topics) => {
|
||||
const [lastTopics, setLastTopics] = useState();
|
||||
const [prevUpdate, setPrevUpdate] = useState();
|
||||
const pushPossible = useNotificationPermissionListener(() => notifier.pushPossible());
|
||||
|
||||
useEffect(() => {
|
||||
const topicsChanged = JSON.stringify(topics) !== JSON.stringify(lastTopics);
|
||||
if (!pushPossible || !topicsChanged) {
|
||||
const nextUpdate = JSON.stringify({ topics, pushPossible });
|
||||
if (topics === undefined || nextUpdate === prevUpdate) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -181,12 +181,12 @@ const useWebPushListener = (topics) => {
|
||||
try {
|
||||
console.log("[useWebPushListener] Refreshing web push subscriptions", topics);
|
||||
await subscriptionManager.updateWebPushSubscriptions(topics);
|
||||
setLastTopics(topics);
|
||||
setPrevUpdate(nextUpdate);
|
||||
} catch (e) {
|
||||
console.error("[useWebPushListener] Error refreshing web push subscriptions", e);
|
||||
}
|
||||
})();
|
||||
}, [topics, lastTopics]);
|
||||
}, [topics, pushPossible, prevUpdate]);
|
||||
|
||||
useEffect(() => {
|
||||
const onMessage = () => {
|
||||
|
||||
Reference in New Issue
Block a user