Safari push notification (OS X Mavericks and Safari 7)

断了今生、忘了曾经 提交于 2020-01-06 14:00:11

问题


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?


回答1:


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..

  1. Web Service URL is protected by https and hostname matches with the CN of the Certificate used. Also please ensure that it is a verified Certificate.
  2. Website push id is valid and is registered via Developer Portal.
  3. Any signing errors are posted at the web service, logging end point. So you might want to get that endpoint up and running before you attempt a push package download.

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.




回答2:


You are required to define following server-side endpoints

  1. webServiceURL/version/pushPackages/websitePushID – location of the
    push package, requested by POST request.

  2. 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.

  3. 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.



来源:https://stackoverflow.com/questions/23007768/safari-push-notification-os-x-mavericks-and-safari-7

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