Pushpad:订阅提示只在用户第一次点击chrome上的启用按钮时出现



你好

我已经检查了PushPad问题的答案:订阅在网站刷新后被删除,但它对我没有帮助。

我在文档后面创建了一个订阅/取消订阅按钮,每次我点击订阅按钮时,Firefox上都会显示提示,所以我想我的流程是正确的,但我不明白为什么它在Chrome上不起作用=>我第一次启动Chrome时才显示提示。

以下是我所做的

pushpad('init', #projectID);
var pushId = $("#main").data("pushid");
var pushSig = $("#main").data("pushsig");
var updateButton = function (isSubscribed) {
var btn = $('#activate-push-notif');
if (isSubscribed) {
btn.html('Unsubscribe');
btn.addClass('subscribed');
} else {
btn.html('Subscribe');
btn.removeClass('subscribed');
}
};
// check whether the user is subscribed to the push notifications and
// initialize the button status (e.g. display Subscribe or Unsubscribe)
pushpad('status', updateButton);
// when the user clicks the button...
$('#activate-push-notif').on('click', function (e) {
e.preventDefault();
// if he wants to unsubscribe
if ($(this).hasClass('subscribed')) {
pushpad('unsubscribe', function () {
updateButton(false);
}, {
uid: pushId,
});
// if he wants to subscribe
} else {
// try to subscribe the user to push notifications
pushpad('subscribe', function (isSubscribed) {
if (isSubscribed) {
updateButton(true);
} else {
updateButton(false);
alert('You have blocked notifications from your browser preferences.');
}
}, {
uid: pushId,
uidSignature: pushSig
});
}
});

谢谢你的帮助。

  1. 请确保将所有代码封装在$(function () { ... }中,以便在页面完全加载时运行代码,否则您的单击处理程序可能无法正常工作
  2. 关键可能是,当你看到Firefox提示时,你会点击">现在不行"(而不是隐藏在▼菜单(。这就是为什么您可以多次看到提示的原因。在其他浏览器中,如Chrome,默认选项为">阻止"(这是一个永久性的阻止,如">Never allow"(
  3. 当用户阻止通知时,由于浏览器限制,您无法再次显示浏览器提示。。。然而,也有一些可供选择的解决方案。例如,您可以根据需要多次显示自定义提示,然后仅当用户对自定义提示表示同意时才显示浏览器提示。请参见此示例。否则,您只能向阻止通知的用户显示提示,要求取消阻止通知。请参见此示例。另一个选项是在页面中显示一个按钮,允许用户在决定订阅通知时订阅通知

最新更新