4

I'm trying to implement safari push notifications on my site using this guide https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/NotificationProgrammingGuideForWebsites/PushNotifications/PushNotifications.html

There is a button on the site and the following JS code:

window.onload = function() {                                                                                               
  var p = document.getElementById('subscribe');                                                                            
  p.onclick = function() {                                                                                                 
    // Ensure that the user can receive Safari Push Notifications.                                                         
    if ('safari' in window && 'pushNotification' in window.safari) {                                                       
      var permissionData = window.safari.pushNotification.permission('MY_REAL_WEBSITE_PUSH_ID');       
      checkRemotePermission(permissionData);                                                                               
    }                                                                                                                      
  };                                                                                                                       

  var checkRemotePermission = function(permissionData) {                                                                   
    console.log(permissionData);                                                                                           
    if (permissionData.permission === 'default') {                                                                         
      // This is a new web service URL and its validity is unknown.                                                        
      window.safari.pushNotification.requestPermission(                                                                    
        'MY_REAL_WEBSERVICE_URL', // The web service URL.                                             
        'MY_REAL_WEBSITE_PUSH_ID',     // 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') {                                                                     
      // The user said 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.                                                               
    }                                                                                                                      
  };                                                                                                                       
}

As a result when I press the button I get request permission.

When I disallow notifications all works as expected: console.log(permissionData); shows permissionData.permission equals denied and I can see site as denied at Safari's Preferences -> Notifications section.

But when I allow notification nothing happens. It seems checkRemotePermission doesn't fire as window.safari.pushNotification.requestPermission's callback.

Any thoughts?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3309314
  • 2,453
  • 2
  • 17
  • 30
  • Your code appears to be a exact copy of that in the documentation. That should work. What is `console.log(permissionData);` logging? – Cerbrus Nov 20 '17 at 09:08
  • I expect to see `permissionData.permission` will equals `granted` but `checkRemotePermission` isn't called after notifications allowance but it's called after notifications ban and I see `permissionData.permission` equals `denied` – user3309314 Nov 20 '17 at 09:20
  • Any progress? I'm have same issue, but i'm think maybe because I test on virtual machine? – Atterratio May 10 '18 at 18:04
  • See if any of these help https://stackoverflow.com/a/20552061/2830850, https://stackoverflow.com/a/25680982/2830850 ? – Tarun Lalwani May 10 '18 at 19:46
  • Seems Apple doesnt allow the user to change its mind:(See Answer 1) https://stackoverflow.com/questions/24596837/osx-push-notifications-for-websites-safari-requestpermission-callback-never-ca#24690429 – Wolfgang Blessen May 15 '18 at 11:25
  • https://github.com/OneSignal/OneSignal-Website-SDK/issues/126 - there was also very similar bug on github – Leo Odishvili May 17 '18 at 08:45

2 Answers2

0

I had a similar problem in a virtual machine, and found a solution for VMWare.

In the configuration .vmx file, you need add something like this:

smbios.reflectHost = "TRUE"
serialNumber = "RM125589AGW"
board-id = "MAC-F22598C8"
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
Atterratio
  • 445
  • 2
  • 9
  • 25
0

Permission is only granted if the push package is successfully validated. If something is wrong, permission is set to denied. You need to check the /log endpoint on the server to know what is wrong.

T3rm1
  • 2,299
  • 5
  • 34
  • 51