PushPad: Subscribe is removed after site refresh

喜你入骨 提交于 2019-12-02 02:31:39

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!