PushPad: la suscripción se elimina después de actualizar el sitio
He integrado PushPad y he logrado que funcione para Push estáticos. Ahora quería combinarlo con algunas funciones PHP y Javascript para que sea dinámico. Aquí está mi código:
<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>
A primera vista, todo funciona bien. Si visito el sitio por primera vez, aparecen todas las alertas, hasta la alerta "UID". Luego, Chrome me pide que acepte las notificaciones automáticas. Hago clic en Permitir y luego aparece la alerta "Suscrito".
Si actualizo el sitio ahora, todo se repite hasta la alerta "Suscrito" (pero Chrome ya no me pide que permita notificaciones push). Pensé que debería aparecer la alerta "Ya suscrito", porque me he suscrito antes, pero no es así.
Agradecería que alguien pudiera ayudar :-)