我正在尝试使用这个指南在我的网站上实现Safari推送通知:https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/NotificationProgrammingGuideForWebsites/PushNotifications/PushNotifications.html
网站上有一个按钮和以下JS代码:
当我按下按钮时,会请求权限。当我拒绝通知时,一切都如预期的那样工作:
window.onload = function() {
var p = document.getElementById('subscribe');
p.onclick = function() {
// Ensure that the user can receive Safari Push Notifications.
if ('safari' in window && 'pushNotification' in window.safari) {
var permissionData = window.safari.pushNotification.permission('MY_REAL_WEBSITE_PUSH_ID');
checkRemotePermission(permissionData);
}
};
var checkRemotePermission = function(permissionData) {
console.log(permissionData);
if (permissionData.permission === 'default') {
// This is a new web service URL and its validity is unknown.
window.safari.pushNotification.requestPermission(
'MY_REAL_WEBSERVICE_URL', // The web service URL.
'MY_REAL_WEBSITE_PUSH_ID', // The Website Push ID.
{}, // Data that you choose to send to your server to help you identify the user.
checkRemotePermission // The callback function.
);
}
else if (permissionData.permission === 'denied') {
// The user said no.
}
else if (permissionData.permission === 'granted') {
// The web service URL is a valid push provider, and the user said yes.
// permissionData.deviceToken is now available to use.
}
};
}
当我按下按钮时,会请求权限。当我拒绝通知时,一切都如预期的那样工作:
console.log(permissionData);
显示permissionData.permission
等于denied,而且我可以在Safari的“偏好设置”->“通知”部分看到网站已被拒绝。但是,当我允许通知时,什么也不发生。似乎checkRemotePermission
没有触发window.safari.pushNotification.requestPermission
的回调函数。有任何想法吗?
console.log(permissionData);
记录了什么? - CerbruspermissionData.permission
等于granted
,但在通知允许后没有调用checkRemotePermission
,但在通知禁止后却被调用了,我看到permissionData.permission
等于denied
。 - user3309314