Im trying to use the new push notifications in Safari. I'm using the following snippet:
var checkRemotePermission = function (permissionData) {
if (permissionData.permission === 'default') {
// This is a new web service URL and its validity is unknown.
console.log("default");
window.safari.pushNotification.requestPermission('https://website.com/','web.com.website.notify',{uid: "TEST"},checkRemotePermission);
}
else if (permissionData.permission === 'denied') {
// The user said no.
console.log("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("yes");
}
};
if ('safari' in window && 'pushNotification' in window.safari) {
var permissionData = window.safari.pushNotification.permission('web.com.website.notify');
checkRemotePermission(permissionData);
}else{
alert("This feature is only available on Mac OS X safari")
}
The problem is that I get no
in my javascript console, because the permission
is denied
. The thing is it never asked, nor has it ever asked before. Its not even in my safari preferences.
Why does safari return denied
without even asking?
This error can happen when testing locally because the address you are testing from isn't part of the allowedDomains
array in the site's pushPackage
. For https://zeropush.com, we added the host lvh.me
to our pushPackage
and ran the server on port 80
, while we were implementing safari push notifications. You then access your development site at lvh.me
and things should behave normally.
You should also implement the logging endpoint for your site described in the safari push notification docs so that you can get any error log information to help you in debugging.
I also wrote a post about implementing safari push notifications that is somewhat ruby centric, but may be of use. https://zeropush.com/blog/implementing-safari-push-notifications-in-osx-mavericks.