Safari push notification (OS X Mavericks and Safar

2019-09-09 02:51发布

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?

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-09 03:03

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.

查看更多
我只想做你的唯一
3楼-- · 2019-09-09 03:04

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.

查看更多
登录 后发表回答