mercredi 5 juin 2019

When receiving new Web Push Notification, the most recent prior to the new one is shown first

I just started tinkering around with the Notification API in one of my React apps, and I've come across something really strange that I haven't been able to Google my way out of. I've allowed notifications from my site, and I am receiving notifications when I trigger them... But every time I receive a new notification, it shows the one received before it first.

So if I've sent a total of 5 notifications, and am now triggering the 6th. It will first show #5, and then #6. If I trigger another one, it'll first show #6, and then #7. The only way to get out of this mess is to manually clear my Notification history, then trigger a new Notification, then it'll only show the new #1. But if I then trigger another one, it'll show #1 before #2 again.

Anyone know why this happens? I haven't been able to find anyone else with the same kind of issue so far.

Here's the code:

notify(item) {
    if (this.state.notificationsEnabled) {
        new Notification("Item found!", {body: item.character + " (" + item.account + ") has found a " + item.name + " at level " + item.level + "! (Current rank: " + item.rank + ")"});
    }
}

toggle() {
    if (!("Notification" in window)) {
        alert("This browser does not support system notifications!");
    } else if (Notification.permission === 'granted') {
        this.setState({ notificationsEnabled: !this.state.notificationsEnabled});
    } else if (Notification.permission !== 'denied') {
        Notification.requestPermission().then((permission) => {
            if (permission === 'granted') {
                this.setState({notificationsEnabled: true});
            }
        });
    }
}

The notify function is triggered manually, and is passed data used by the notification. The toggle function is bound to an on/off switch that the user switches on to enable notifications to be spawned if permission has not been granted before, otherwise it just switches the state variable, which controls if data passed to notify will create an actual notification or not.




Aucun commentaire:

Enregistrer un commentaire