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

Subscribing - consider existing subscription #25

Open
arogachev opened this issue Oct 10, 2016 · 2 comments
Open

Subscribing - consider existing subscription #25

arogachev opened this issue Oct 10, 2016 · 2 comments

Comments

@arogachev
Copy link
Contributor

arogachev commented Oct 10, 2016

When page initially loaded, subscribe button is shown regardless of user already subscribed or not, This is not user friendly interface.

At least we need to check if user already subscribed and show some information about that (for example display "Unsubscribe" button instead).

@arogachev arogachev changed the title Subscribing - consider database state and multiple subscriptions Subscribing - consider existing subscription Oct 10, 2016
@safwanrahman
Copy link
Owner

@arogachev So the thing is, user can subscribe from multiple browser.
We do not show the message if the user already subscribed from the the same browser.
Does it make sense?

@elineda
Copy link

elineda commented Sep 9, 2019

I found a solution for that.
You need to inject a array of endpoint on your view

    pushs = PushInformation.objects.filter(user=request.user).all()

    endpoints=[]

    for push in pushs:

        endpoint = push.subscription.endpoint
        endpoints.append(endpoint)
    end= json.dumps({"point":endpoints})
    if pushs:
        pushactif=1
    else:
        pushactif=0
    return render(request, 'mytemplate.html', { 'pushactif':pushactif, 'end':end})

After that you can write a little code in js to check if the end point of our registration match any of endpoint found on server.

    $(function () {
        var active = {{ pushactif }};
        var end = `{{ end|safe }}`;
        var endpoints = JSON.parse(end);
        var serviceWorker = document.querySelector('meta[name="service-worker-js"]').content;
        navigator.serviceWorker.register(serviceWorker)
            .then(
                function(reg) {
                    registration = reg;
                    var navpoint;
                    registration.pushManager.getSubscription().then(function (nav) {
                        navpoint=  nav.endpoint;
                        let ok =0;
                        if (active === 1){
                            for (let i=0; i<endpoints.point.length; i++){
                                if (endpoints.point[i]===navpoint){
                                    ok=1;
                                }
                            }
                        }
                        if (ok===1){
                            subBtn.textContent = 'Unsubscribe to Push Messaging';
                            subBtn.disabled = false;
                            isPushEnabled = true;
                        }
                    })
                }
            );`

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

3 participants