我编写了代码来获取 GCM id 以发送 chrome 推送通知。我写了下面的代码来获取 GCM id。
var ChromePushManager = function (serviceWorkerPath, callback) {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register(serviceWorkerPath).then(function (registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
registration.pushManager.subscribe({userVisibleOnly: true}).then(function (subscription) {
console.log("subscription.subscriptionId: ", subscription.subscriptionId);
console.log("subscription.endpoint: ", subscription.endpoint);
// TODO: Send the subscription subscription.endpoint
// to your server and save it to send a push message
// at a later date
var register = ChromePushManager.getRegistrationId(subscription);
callback(null, register);
});
}).catch(function (err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
} else {
callback('Service workers aren\'t supported in this browser.', null);
}
};
ChromePushManager.getRegistrationId = function (pushSubscription) {
if (pushSubscription.subscriptionId) {
return pushSubscription.subscriptionId;
}
var endpoint = 'https://android.googleapis.com/gcm/send/';
parts = pushSubscription.endpoint.split(endpoint);
if (parts.length > 1)
{
console.log("RegistrationId : " + parts[1]);
return parts[1];
}
}
var chromePushManager = new ChromePushManager('../js/service-worker.js', function (error, registrationId) {
if (error !== null) {
console.log(error);
}
});
它在 http://localhost:8080 中运行良好.我在服务器上传了这段代码。我试图在服务器中执行代码。我遇到了错误。
ServiceWorker 注册失败:DOMException:只允许安全来源(参见:https://goo.gl/Y0ZkNV)。
我不知道这个错误是什么意思。你能不能解释一下这个错误是什么意思,并给我一个在我的服务器端解决这个错误的解决方案。
谢谢。
我是一名优秀的程序员,十分优秀!