2

I've integrated PushPad and managed to get it working for static Push's. Now I wanted to combine it with some PHP and Javascript-Functions to make it dynamic. Here is my code:

<script>
    (function(p,u,s,h,x){p.pushpad=p.pushpad||function(){(p.pushpad.q=p.pushpad.q||[]).push(arguments)};h=u.getElementsByTagName('head')[0];x=u.createElement('script');x.async=1;x.src=s;h.appendChild(x);})(window,document,'https://pushpad.xyz/pushpad.js');

    //Install Pushpad
    pushpad('init', myprojectnumber);
    alert("Pushpad initialised");
    //Check subscribe-status
    pushpad('status', function (isSubscribed, tags){
        //User is already subscribed
        if (isSubscribed){
            alert("Already subscribed");
        //User has not subscribed
        }else{
            alert("Not subscribed");
            //Check in database if this logged-in-user has already subscribed with 5 different devices, if not generate UID and UID_SIGNATURE
            var username = $('#username_show').html();

            alert('Username: ' + username);
            $.ajax
            ({                                        
                type: "POST",
                data: {username: username},
                dataType: "json",
                url: "setNotifications.php",
                cache: false,
                success: function(data)
                {
                    alert("Ajax successfull. UID generated.");

                    if (data.uid != 0){
                        //Set UID and UID-SIGNATURE
                        pushpad('uid', data.uid, data.uid_signature);
                        alert('UID:' + data.uid);
                        //Subscribe
                        pushpad('subscribe', function(isSubscribed){
                            if (isSubscribed){
                                alert("Subscribed");
                            }else{
                                alert("Notifications blocked");
                            }

                        });
                    //Already 5 devices subscribed
                    }else{
                        alert("Already 5 devices");
                    }
                },
                error: function()
                {
                    alert('Error');
                }
            });
        }
    });
</script>

At first sight everything works fine. If I visit the site for the first time all alerts pop up, up to the "UID"-alert. Then I am asked by Chrome to accept push-notifications. I click allow and then the alert "Subscribed" pops up.

If I refresh the site now, everything repeats up to the "Subscribed"-alert (but I am not asked to allow push-notifications by Chrome anymore). I would have thought that the alert "Already subscribed" should show up, because I have subscribed before, but it doesn't.

Would appreciate it if somebody could help :-)

Fabian H.
  • 133
  • 1
  • 2
  • 8
  • Also in my PushPad-Project the browser seems to be subscribed with the UID I gave it, but I can't send any pushs to it. – Fabian H. May 24 '16 at 23:06
  • Regarding your comment: from the dashboard try to 1. send a notification to evryone and see if you receive something 2. send a notification filling in the user id filter in the dashboard (**if that uid has been saved in the database you should see a suggestion/autocomplete when you start typing it in the input field**) – collimarco May 25 '16 at 08:53

1 Answers1

1

The problem is that status returns the subscription status for the current user (uid). You only set the uid for subscribe and status doesn't know about it.

This is what happens in your code:

  1. The user is actually subscribed to push notifications
  2. You refresh the page
  3. status checks whether there is a subscription for the uid = null (not for the actual user id because is not set yet!)
  4. It returns false because there isn't a subscription with uid = null
  5. The subscription process is triggered again

The solution is to move all Pushpad code inside your AJAX success callback. So you the following sequence:

  1. pushpad('init')
  2. Your ajax callback
  3. on success you set the uid with pushpad('uid')
  4. pushpad('status')
  5. inside the pushpad('status') callback use pushpad('subscribe')

BTW why do you use an AJAX callback to get the uid? If the user is logged in on your website when you render the page containing the Pushpad code you probably already know the uid (e.g. the user email or the id of the user in your database). You can do something like pushpad('uid', '<?= $uid ?>', '<?= $uid_signature ?>');

collimarco
  • 34,231
  • 36
  • 108
  • 142
  • Thanks for yout help :-) I've got a false workflow in my code, but your adviced helped. But now I've got another problem. I've subscribed with my username as UID and can see the subscription in my pushpad project and on my site it also says 'subscribed' as status. But I can't receive any notifications sent. When I send any notification (doesn't matter if to __all__ or just to my __uid__ ), it says `0 successfully sent -> 0 opened` ? – Fabian H. May 25 '16 at 19:54
  • The comment above concerns to Google Chrome. Now I have also logged in to my website account on Firefox and set the same UID as in Google Chrome. Sending a Push to the UID again, it works with Firefox but not with Chrome. – Fabian H. May 25 '16 at 20:14
  • I've found another strange thing. When I go into the __Chrome Developer Tools__ under __Ressources__ to __Service Workers__ there is a button labeled **Emulate Push Event** . After I clicked this button, all the "Test Pushs" I've send to the UID showed up at once, as if they have been in a waiting queue and weren't delivered by the __Service Worker__ ? – Fabian H. May 25 '16 at 20:33
  • @FabianH. Yes, that is the expected behavior. When a push event is triggered the service worker fetches all new notification for that device from the server (Pushpad) and displays the notifications. – collimarco May 26 '16 at 10:45
  • Yes, but the notifications __are not displayed automatically__. Using Firefox I receive the notifications exactly when I send them (and they are displayed in that moment). But Chrome only displays the notifications when I click __Emulate Push Event__ in the Developer Tools. But my website users should receive them without having to go into the Developer Tools and click the button. – Fabian H. May 26 '16 at 12:38
  • @FabianH. That means that the push event is not triggered in Chrome. Probably you have entered the wrong credentials for Google Cloud Messaging and their server don't forward the push event to the browser. Check the "sender id" in the manifest and the **"GCM Api key" in the project settings** on Pushpad. If you read `0 successfully sent` and you are subscribed with Chrome the reason is that. – collimarco May 26 '16 at 13:08
  • @FabianH. The only other reason I can think of is that the Chrome subscription has expired. For example you have called `pushpad('subscribe')`, then you have blocked notifications from browser preferences (the subscription expires), then you have allowed them again but you have not called `subscribe` again (in this case calling `subscribe` again would fix the issue: you can call it as many times as you want even if the user is already subscribed). – collimarco May 26 '16 at 13:18
  • As this is a new problem I've started a new question: http://stackoverflow.com/questions/37470521/pushpad-chrome-doesnt-display-push-notifications – Fabian H. May 26 '16 at 20:37