Miguel Herreros Cejas
Miguel Herreros Cejas

Reputation: 674

Web Push Django in Safari

I am using push notifications in Django, I am using the library https://github.com/safwanrahman/django-webpush

It work correctly in Chrome and Firefox, but in Safari I have a JS error.

   TypeError: undefined is not an object (evaluating 'reg.pushManager.getSuscription')
   -suscribe
   -Anonymus Function

The code is this:

function subscribe(reg) {
  // Get the Subscription or register one
  reg.pushManager.getSubscription().then(
   function(subscription) {
     var metaObj, applicationServerKey, options;
     // Check if Subscription is available
     if (subscription) {
       return subscription;
     }

     metaObj = document.querySelector('meta[name="django-webpush-vapid-key"]');
     applicationServerKey = metaObj.content;
     options = {
     userVisibleOnly: true
     };
     if (applicationServerKey){
       options.applicationServerKey = urlB64ToUint8Array(applicationServerKey)
     }
     // If not, register one
     reg.pushManager.subscribe(options)
       .then(
       function(subscription) {
        postSubscribeObj('subscribe', subscription,
          function(response) {
            // Check the information is saved successfully into server
            if (response.status === 201) {
              // Show unsubscribe button instead
              subBtn.textContent = 'Eliminar suscripción';
              subBtn.disabled = false;
              isPushEnabled = true;
              showMessage('La suscripción se ha eliminado correctamente');
            }
          });
      })
    .catch(
      function() {
        console.log('Subscription error.', arguments)
      })
});
 }

Upvotes: 1

Views: 549

Answers (1)

Saket Saumya
Saket Saumya

Reputation: 36

That's because for invoking push notification in Safari or IOS devices, APNS (Apple Push Notification Service) is required and maintainers of django-web-push have not integrated them yet.

You will need to add APNS support by yourself by adding a fallback option for IOS devices and browsers or you can use any other apns push notification service.

Upvotes: 2

Related Questions