Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom callbacks on subscription #133

Open
azmeuk opened this issue Sep 27, 2023 · 1 comment
Open

Custom callbacks on subscription #133

azmeuk opened this issue Sep 27, 2023 · 1 comment

Comments

@azmeuk
Copy link

azmeuk commented Sep 27, 2023

This would be very convenient to execute custom code when users subscribe or unsubscribe to notifications.
I suggest implementing a callback mechanism, for instance by adding a callback parameter in to the subscribe and unsubscribe functions, and execute the callbacks in or instead of reg.pushManager.subscribe(options).then and subscription.unsubscribe().then for instance.
What do you think?

@iragm
Copy link

iragm commented Sep 15, 2024

I worked around this by adding the following to my template's js:

    const targetNode = document.getElementById('webpush-subscribe-button');
    // Create a callback function that executes when mutations are observed
    const callback = function(mutationsList) {
        for (let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                if (targetNode.textContent == "Subscribe to Push Messaging") {
                    // do something here
                   console.log('subscribed');
                } else {
                    // do something different here
                    console.log(targetNode.textContent);
                }
            }
        }
    };
    // Create an observer instance linked to the callback function
    const observer = new MutationObserver(callback);
    // Define the configuration for the observer
    const config = { childList: true, subtree: true };
    // Start observing the target node for configured mutations
    observer.observe(targetNode, config);

This does not take into account i18n, but it works in my app. It would be much cleaner to have this built in as you suggest, but it does not seem like this repo is very maintained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants