Web推送通知错误

时间:2018-01-08 17:21:01

标签: javascript push-notification

我目前正在开发一个Progressan Web App,一个Messanger。 我尝试实施网络推送通知,但收到错误:

Error: The subscription p256dh value should be 65 bytes long.
    at Object.encrypt (script.js:67350)
    at Object.WebPushLib.generateRequestDetails (script.js:67851)
    at Object.WebPushLib.sendNotification (script.js:67921)
    at script.js:427
    at Array.forEach (<anonymous>)
    at Object.success (script.js:418)
    at fire (script.js:57068)
    at Object.fireWith [as resolveWith] (script.js:57198)
    at done (script.js:63023)
    at XMLHttpRequest.<anonymous> (script.js:63265)

不幸的是,我无法在互联网上找到解决此问题的方法。

在这里,我提供通知,订阅用户通知并将此订阅保存到mongodb。

function urlBase64ToUint8Array(base64String) {
            const padding = '='.repeat((4 - base64String.length % 4) % 4);
            const base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/');

            const rawData = window.atob(base64);
            const outputArray = new Uint8Array(rawData.length);

            for (var i = 0; i < rawData.length; ++i) {
                outputArray[i] = rawData.charCodeAt(i);
            }
            return outputArray;
        }

    function offerNotification() {
        if ("Notification" in window && "PushManager" in window && "serviceWorker" in navigator) {
            Notification.requestPermission().then(function(permission){
                if (permission === "granted") {
                    var subscribeOptions = {
                        userVisibleOnly: true,
                        applicationServerKey: urlBase64ToUint8Array(
                            "BFj8CT2z3z40tC-qDDVBj4EnhV345Bx53H_cYkF92w89_7G1dG8Gv4G0FhEACXSFOKf3gvC0eu0VbRRk1pSRpWU"
                        )
                    };
                    navigator.serviceWorker.ready.then(function(registration) {
                        return registration.pushManager.subscribe(subscribeOptions);
                    }).then(function(subscription) {
                        var fetchOptions = {
                            method: "post",
                            headers: new Headers({
                                "Content-Type": "application/json"
                            }),
                            body: JSON.stringify(subscription)
                        };
                        return fetch("/add-subscription/", fetchOptions);
                    });
                }
            });
        }
    };

subscriptions.js

var webpush = require("web-push");

/**
 * Sends a push message to all subscriptions
 */

function notify(pushPayload) {
    var pushKeys;
    $.getJSON("/pushkeys/", function (data) {
        pushKeys = data;
        pushPayload = JSON.stringify(pushPayload);
        webpush.setGCMAPIKey(pushKeys[0].GCMAPIKey);
        webpush.setVapidDetails(
            pushKeys[0].subject,
            pushKeys[0].publicKey,
            pushKeys[0].privateKey
        );

        $.getJSON("/subscriptions/", function (subscriptions) {
            subscriptions.forEach(function(subscription) {
                webpush
                    .sendNotification(subscription, pushPayload)
                    .then(function() {
                        console.log("Notification sent");
                    })
                    .catch(function(err) {
                        console.log(err);

                        console.log("Notification failed");
                    });
            });
        });
    });
};

当新用户注册时,会通知所有其他用户。

 notify({
      type: "user-registration",
      user: user
 });

和Serviceworker中的监听器

self.addEventListener("push", function(event) { console.log("hier");
    var data = event.data.json(); console.log(data);
    if (data.type === "user-registration") {
        var user = data.user;
        event.waitUntil(
            self.registration.showNotification("Neur Nutzer :", {
                body: user.nickname + " hat sich registriert.",
                icon: "/images/icons/icon-64x64.png",
                tag: "user-registration",
                vibrate: [500,110,500,110,450,110,200,110,170,40,450,110,200,110]
            })

        );
    }
});

非常感谢任何帮助 提前致谢

0 个答案:

没有答案