Shams Reza Shams Reza - 1 month ago 20
Javascript Question

Title:Getting permission denied even before asking allow for Safari Desktop Notification

First I created pushId: web.com.domainname
then created pushPackage having icon.iconset,website.json,manifest.json and made it zip and kept in pulic flder.
then I wrote javascript code to ask permission .



window.onload = function () {
if ('safari' in window && 'pushNotification' in window.safari) {
var permissionData = window.safari.pushNotification.permission('web.com.domainname');
checkRemotePermission(permissionData);
}
};
var checkRemotePermission = function (permissionData) {
console.log(permissionData);
if (permissionData.permission === 'default') {

window.safari.pushNotification.requestPermission(
'https://domainname.com',
'web.com.domainname', {},
checkRemotePermission
);
} else if (permissionData.permission === 'denied') {
console.log('denied');

} else if (permissionData.permission === 'granted') {
console.log('granted');
}


};





when I run this code I don't see any permission allow notification but in console it tells permission denied and deviceToken null. What is missing and what extra I have to do I am not getting please help me.
Thanks in advance...!

Answer

Its Looks like you already done the first part of safari push conversion . Now for The request permission you should write a funtion in javascript Tag is

 document.body.onload = function() {
         if ('safari' in window && 'pushNotification' in window.safari) {
                    var permissionData = window.safari.pushNotification.permission('Push Id');
                    checkRemotePermission(permissionData);
                }
            };

            var checkRemotePermission = function (permissionData) {
                if (permissionData.permission === 'default') {
                    // This is a new web service URL and its validity is unknown.
                    console.log('making decision.');

                    window.safari.pushNotification.requestPermission(
                            'https://pushconversions.com', // The web service URL.The URL though which safari can call,post data if any error or Success message come.
                            'PushId',     // The Website Push ID.
                            {"login":"email Id",
                                "frequentID":"123456"}, // 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.
                    console.log('The user denied');

                }
                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('and the user said yes.');
                    console.log(permissionData.deviceToken);
                }
            };

Then It Provide An DeviceToken that you have to Store in DB. To store the Device Token There will be a post method from safari send to your server with url:=post('/v1/devices/{deviceToken}/registrations/web.com.pushconversion', function($token) {dd($token);}); When the Subscriber unsubscribe then a Delete method will be called and you have to delete the token from your DB that is:-DELETE('/v1/devices/{deviceToken}/registrations/web.com.pushconversion', function($token) {dd($token);}); Now you have the Device Token ,Here by Now you are able to send the push notification to the safari subscriber.