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 :-)