Показывать, что функция уведомления не запускается даже после вызова слушателя событий pu sh - PullRequest
1 голос
/ 30 января 2020

Используя publi c и пару секретных ключей, пытающихся отправить уведомление pu sh через Интернет. В этом файле я получаю детали уведомления pu sh в файле сервисного работника, но дело в том, что самостоятельная регистрация показывает функцию уведомления, а не вызываемое событие после запуска pu sh.

var url = 'token url';
self.addEventListener('push', function (event) {
    event.waitUntil(
        fetch(url, {
            method: 'post',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                subscription_id: Math.random(),
                url: 'deliverable url'
            })
        }).then(function (response) {
            if (response.status !== 200) {
                throw new Error();
            }

            // Examine the text in the response  
            return response.json().then(function (data) {
                if (data.error || !data.notification) {
                    console.log('The API returned an error.', data.error);
                    throw new Error();
                }
                console.log('first');
                var title = data.notification.title;
                var message = data.notification.message;
                var icon = data.notification.icon;
                var image = data.notification.image;
                var not_url = data.notification.url;
                return self.registration.showNotification(title, {
                    body: message,
                    icon: icon,
                    image: image,
                    data: {
                        url: not_url
                    }
                });
                console.log('second');
            });
        }).catch(function (err) {
            console.log('Unable to retrieve data', err);
            var title = 'An error occurred';
            var message = 'We were unable to get the information for this push message';
            var icon = 'img/pwa.jpg';
            var notificationTag = 'notification-error';
            return self.registration.showNotification(title, {
                body: message,
                icon: icon,
                tag: notificationTag
            });
        })
    );
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...