服务工作者事件waitUntil()传递了一个以' TypeError拒绝的承诺:b为null'

时间:2016-10-26 11:36:05

标签: firebase push-notification firebase-cloud-messaging service-worker

这是我的服务工作者文件,显示推送通知:

importScripts('https://www.gstatic.com/firebasejs/3.5.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.0/firebase-messaging.js');

var config = {
    apiKey: "my_key",
    authDomain: "my_domain.firebaseapp.com",
    databaseURL: "https://my_db.firebaseio.com",
    storageBucket: "my_bucket.appspot.com",
    messagingSenderId: "my_sender_id"
};
firebase.initializeApp(config);


var messaging = firebase.messaging();

messaging.setBackgroundMessageHandler(function (payload) {

    var notificationTitle = 'Hello World';
    var notificationOptions = {
        body: 'Test push notification'
    };

    return self.registration.showNotification(notificationTitle,notificationOptions);

});

这是我的html,它注册了服务工作者:

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>TODO write content</div>
        <script src="https://www.gstatic.com/firebasejs/3.5.0/firebase.js"></script>
        <script>
             var config = {
                apiKey: "my_key",
                authDomain: "my_domain.firebaseapp.com",
                databaseURL: "https://my_db.firebaseio.com",
                storageBucket: "my_bucket.appspot.com",
                messagingSenderId: "my_sender_id"
            };
            firebase.initializeApp(config);
        </script>

        <script>

            var messaging = firebase.messaging();
            var ser = '/serviceworker.js';
            if ('serviceWorker' in navigator) {
                // Register service worker
                navigator.serviceWorker.register('./public/serviceworker.js').then(function (reg) {
                    messaging.useServiceWorker(reg);
                    messaging.requestPermission()
                            .then(function () {
                                console.log('Notification permission granted.');
                                return messaging.getToken()
                                        .then(function (currentToken) {
                                            if (currentToken) {
                                                console.log('in if', currentToken);
                                                if (window.XMLHttpRequest) {
                                                    xhttp = new XMLHttpRequest();
                                                } else {
                                                    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
                                                }
                                                xhttp.onreadystatechange = function () {
                                                    if (this.readyState == 4 && this.status == 200) {

                                                    }
                                                };
                                                xhttp.open("POST", "/notification", true);
                                                xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
                                                xhttp.send(JSON.stringify({token: currentToken}));
                                            } else {
                                                console.log('in else', currentToken);
                                            }

                                        }).catch(function (err) {
                                    console.log('An error occurred while retrieving token. ', err);
                                });
                            })
                            .catch(function (err) {
                                console.log('Unable to get permission to notify. ', err);
                            });

                    messaging.onMessage(function (payload) {
                        var options = {
                            body: payload.data.body
                        }
                        var notificationObj = new Notification(payload.data.title, options);
                   });
                }).catch(function (err) {
                    console.error("SW registration failed with error " + err);
                });
            }

        </script>
    </body>
</html>

一切正常,但是当我第一次加载页面时,我在允许通知权限后看到以下错误。我正在使用FCM进行推送通知。

Service worker event waitUntil() was passed a promise that rejected with 'TypeError: b is null'.

1 个答案:

答案 0 :(得分:0)

这是Firebase Messaging的fixed in version 3.5.1