我已经集成了 PushPad 并设法让它为静态推送工作。现在我想将它与一些 PHP 和 Javascript-Functions 结合起来使其动态化。这是我的代码:
<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>
乍一看,一切正常。如果我第一次访问该站点,所有警报都会弹出,直到“UID”警报。然后我被 Chrome 要求接受推送通知。我单击允许,然后弹出“已订阅”警报。
如果我现在刷新网站,一切都会重复到“订阅”警报(但我不再被要求允许 Chrome 推送通知)。我原以为应该显示“已订阅”警报,因为我以前订阅过,但没有。
如果有人可以提供帮助将不胜感激:-)