PushPad:在网站刷新后删除订阅 [英] PushPad: Subscribe is removed after site refresh

查看:227
本文介绍了PushPad:在网站刷新后删除订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整合了PushPad,并设法让它在静态Push中工作。现在我想将它与一些PHP和Javascript函数结合起来,使其具有动态性。
这里是我的代码:

 < script> 
(function(p,u,s,h,x){p.pushpad = p.pushpad || function(){(p.pushpad.q = p.pushpad.q || [])。push (参数)}; H = u.getElementsByTagName( '头')[0]; X = u.createElement( '脚本'); x.async = 1; x.src = S; h.appendChild(X);} )(窗口,文件,的 'https://pushpad.xyz/pushpad.js');

//安装Pushpad
pushpad('init',myprojectnumber);
alert(Pushpad initialised);
//检查订阅状态
pushpad('status',function(isSubscribed,tags){
//用户已经订阅
if(isSubscribed){
警报(已订阅);
//用户未订阅
}其他{
alert(未订阅);
//检入数据库,如果没有生成UID和UID_SIGNATURE
var username = $('#username_show')。html();

alert('用户名:' + username);
$ .ajax
({
type:POST,
data:{username:username},
dataType:json,
url:setNotifications.php,
cache:false,
success:function(data)
{
alert(Ajax successfull。UID生成的)。

if(data.uid!= 0){
//设置UID和UID-SIGNATURE
pushpad('uid',data.uid,data.uid_signature);
alert('UID:'+ data.uid);
//订阅
pushpad('subscribe',function(isSubscribed){
if(isSubscribed){
alert(Subscribed);
} else {
alert(Notifications blocked);
}

});
//已订购5个设备
}其他{
alert(已有5个设备);
$ b $,
错误:function()
{
alert('Error');
}
});
}
});
< / script>

乍一看,一切正常。如果我第一次访问该网站,所有警报弹出,直到UID - 设置。然后,Chrome会要求我接受推送通知。我点击允许,然后弹出订阅提醒。



如果我现在刷新网站,所有内容都重复到订阅-alert(但我不是要求Chrome允许推送通知)。我会认为已经订阅的警报应该显示出来,因为我已经订阅过,但它没有。



如果有人可以提供帮助,将不胜感激: - )

解决方案

问题在于 status 返回订阅状态为当前用户(uid)。您只需设置用于订阅的uid,而状态不知道它。



这就是您的代码中发生的事情:


  1. 用户实际订阅了推送通知

  2. 刷新页面

  3. 状态检查是否存在对uid = null 的订阅(不是针对实际用户标识,因为尚未设置!)
  4. >
  5. 它返回false,因为没有使用uid = null

  6. 订阅订阅过程是再次触发

解决方案是将您的AJAX成功回调中的所有Pushpad代码移出。所以你按以下顺序:


  1. pushpad('init')

  2. 你的ajax回调
  3. li>
  4. 成功后,您使用pushpad设置uid('uid')

  5. pushpad('status')

  6. ('status')回调使用pushpad('subscribe')

顺便说一句,你为什么使用AJAX回调来获取的uid?如果用户在呈现包含Pushpad代码的页面时在您的网站上登录,那么您可能已经知道uid(例如,用户电子邮件或数据库中用户的ID)。你可以做一些像 pushpad('uid','< = = $ uid?>','<?= $ uid_signature?>');


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

解决方案

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 ?>');

这篇关于PushPad:在网站刷新后删除订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆