pusher / push-notifications-web

Beams Browser notifications
MIT License
39 stars 20 forks source link

Experiment: let tabs decide if notifications show/how clicks are handled #59

Closed TomKemp closed 4 years ago

TomKemp commented 4 years ago
// In some apps, these two things are probably the inverse of eachother. Maybe they could be combined? `isNotificationOwnedByThisPage`
// Note: the filtering can only be done by the focused tab, click listening can be done by any tab

beamsClient.addNotificationFilter(notification => {
    // Should the given notification be shown if this tab has focus?
    // The in-focus tab is asked. The notification is dropped at the first filter to say no (false)
    return getOrderId() !== notification.data.order_id
})

beamsClient.addNotificationClickHandler(notification => {
    // Should clicking the given notification give focus to this tab?
    // The first listener/tab that says yes (true) gets the focus, and the rest aren't asked

    if (getOrderId() === notification.data.order_id) {
        // Update page 
        // E.g. navigate to the message that the notification corresponds to

        // Tell the Beams Service Worker that this tab should be given the focus
        return true
    }
    return false
})
// Maybe rather than the return value determining the focus, the function could receive an
// event with `event.takeFocus()` grabbing the focus.