I have created the push package with valid certificate and hosted on server with valid SSL certificate but problem is that javascript always shows the denied message even before prompting to user. I am using the java script code from apple tutorial with valid website push id & webservice url etc
document.body.onload = function() {
// Ensure that the user can receive Safari Push Notifications.
if ('safari' in window && 'pushNotification' in window.safari) {
var permissionData = window.safari.pushNotification.permission('web.com.example.domain');
checkRemotePermission(permissionData);
}
};
var checkRemotePermission = function (permissionData) {
if (permissionData.permission === 'default') {
// This is a new web service URL and its validity is unknown.
window.safari.pushNotification.requestPermission(
'https://domain.example.com', // The web service URL.
'web.com.example.domain', // 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') {
alert('denied');
// The user said no.
}
else if (permissionData.permission === 'granted') {
alert('granted');
// The web service URL is a valid push provider, and the user said yes.
// permissionData.deviceToken is now available to use.
}
};
The problem is that I get 'denied' alert in my javascript , 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?
You are required to define following server-side endpoints
webServiceURL/version/pushPackages/websitePushID – location of the
push package, requested by POST request.
webServiceURL/version/devices/deviceToken/registrations/websitePushID – when an user grants a permission or later updates his permission
level, a POST request is sent. When user removes the permission for push notifications, a DELETE request is sent.
webServiceURL/version/log – when an error occurs a POST request is made to this endpoint
I suggest you to fork this github repo, It includes very good documentation and these server-side endpoints can be defined using .htaccess file.
Can you confirm that the website push id and the web service URL that you have specified is valid and existent? If it is not a valid service url or website push id, Safari will deny it without even prompting.
I implemented safari push end to end and some things you should watch out for are..
Also i noticed that Apple doesn't provide a sandbox environment for Safari Push. This means that you will need to register a website push id for production and one for pre-production, so that you can keep them separate for testing purposes.
Hope that helps.