Google chrome 从GCM到Chrome。一般帮助。无法订阅/没有发件人id
因此,我正在VS2013中与.NET一起工作,并试图找出如何让云消息(GCM)在Chrome上工作 我想让它像这样工作: 所以我不想使用Android,它只需要像上面的示例一样工作,在Chrome中工作,而不需要在商店中安装扩展/webapp。 我尝试了许多样本和指南,并试图改变它,但我无法得到任何工作 自动取款机。我正在使用以下示例代码: 但我收到两条错误消息: 1:“无法订阅推送” 2:“异常:注册失败–未提供发件人id。” 我知道Google Console中的发送者id=项目编号,API密钥在凭据下–我已多次尝试使用服务器API密钥和浏览器API密钥。 此外,还启用了针对Chrome和Android的云消息API Manifest.jsonGoogle chrome 从GCM到Chrome。一般帮助。无法订阅/没有发件人id,google-chrome,google-cloud-messaging,Google Chrome,Google Cloud Messaging,因此,我正在VS2013中与.NET一起工作,并试图找出如何让云消息(GCM)在Chrome上工作 我想让它像这样工作: 所以我不想使用Android,它只需要像上面的示例一样工作,在Chrome中工作,而不需要在商店中安装扩展/webapp。 我尝试了许多样本和指南,并试图改变它,但我无法得到任何工作 自动取款机。我正在使用以下示例代码: 但我收到两条错误消息: 1:“无法订阅推送” 2:“异常:注册失败–未提供发件人id。” 我知道Google Console中的发送者id=项目编号,AP
{
"name": "Push Demo",
"short_name": "Push Demo",
"manifest_version": 2,
"version": "0.0.0.3",
"browser_action": {
"default_icon": "images/icon-192x192.png"
},
"display": "standalone",
"permissions": [
"gcm",
"storage"
],
"gcm_sender_id": "94512349348",
"gcm_user_visible_only": true
}
在“chrome://serviceworker-internals“它显示为已注册,当我尝试推送时,它会显示:
控制台:{“lineNumber”:4,“message”:“接收到推送消息”,“message_level”:1,“sourceIdentifier”:3,“sourceURL”:“localhost:4724/service worker.js”}
我无法订阅。每当它进入函数subscribe()时,它总是告诉我“无法订阅”,并且它可能是错误的发件人ID或gcm_user_visible_-我无法理解,因为我确定输入了正确的信息
来自Main.js的一些代码
function subscribe() {
// Disable the button so it can't be changed while
// we process the permission request`enter code here`
var pushButton = document.querySelector('.js-push-button');
pushButton.disabled = true;
navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.subscribe({ userVisibleOnly: true })
.then(function (subscription) {
// The subscription was successful
isPushEnabled = true;
pushButton.textContent = 'Disable Push Messages';
pushButton.disabled = false;
return sendSubscriptionToServer(subscription);
})
.catch(function (e) {
if (Notification.permission === 'denied') {
window.Demo.debug.log('Permission for Notifications was denied');
pushButton.disabled = true;
} else {
// A problem occurred with the subscription, this can
// often be down to an issue or lack of the gcm_sender_id
// and / or gcm_user_visible_only
window.Demo.debug.log('Unable to subscribe to push.', e);
pushButton.disabled = false;
pushButton.textContent = 'Enable Push Messages';
}
});
});
function initialiseState() {
// Are Notifications supported in the service worker?
if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
window.Demo.debug.log('Notifications aren\'t supported.');
test.textContent = test.textContent + ' Notifications not supported; ';
return;
}
test.textContent = test.textContent + ' initState; ';
// Check the current Notification permission.
// If its denied, it's a permanent block until the
// user changes the permission
if (Notification.permission === 'denied') {
window.Demo.debug.log('The user has blocked notifications.');
test.textContent = test.textContent + ' DENIED; ';
return;
}
// Check if push messaging is supported
if (!('PushManager' in window)) {
window.Demo.debug.log('Push messaging isn\'t supported.');
return;
}
// We need the service worker registration to check for a subscription
navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) {
// Do we already have a push message subscription?
serviceWorkerRegistration.pushManager.getSubscription()
.then(function (subscription) {
// Enable any UI which subscribes / unsubscribes from
// push messages.
var pushButton = document.querySelector('.js-push-button');
pushButton.disabled = false;
if (!subscription) {
// We aren’t subscribed to push, so set UI
// to allow the user to enable push
return;
}
// Keep your server in sync with the latest subscription
sendSubscriptionToServer(subscription);
// Set your UI to show they have subscribed for
// push messages
pushButton.textContent = 'Disable Push Messages';
isPushEnabled = true;
})
.catch(function (err) {
window.Demo.debug.log('Error during getSubscription()', err);
});
});
}
'use strict';
self.addEventListener('push', function (event) {
console.log('Received a push message', event);
var title = 'Yay a message.';
var body = 'We have received a push message.';
var icon = '/images/icon-192x192.png';
var tag = 'simple-push-demo-notification-tag';
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag
})
);
});
self.addEventListener('notificationclick', function (event) {
console.log('On notification click: ', event.notification.tag);
// Android doesn’t close the notification when you click on it
// See: http://crbug.com/463146
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
type: "window"
}).then(function (clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow('/');
}));
});
服务工作者.js
function subscribe() {
// Disable the button so it can't be changed while
// we process the permission request`enter code here`
var pushButton = document.querySelector('.js-push-button');
pushButton.disabled = true;
navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.subscribe({ userVisibleOnly: true })
.then(function (subscription) {
// The subscription was successful
isPushEnabled = true;
pushButton.textContent = 'Disable Push Messages';
pushButton.disabled = false;
return sendSubscriptionToServer(subscription);
})
.catch(function (e) {
if (Notification.permission === 'denied') {
window.Demo.debug.log('Permission for Notifications was denied');
pushButton.disabled = true;
} else {
// A problem occurred with the subscription, this can
// often be down to an issue or lack of the gcm_sender_id
// and / or gcm_user_visible_only
window.Demo.debug.log('Unable to subscribe to push.', e);
pushButton.disabled = false;
pushButton.textContent = 'Enable Push Messages';
}
});
});
function initialiseState() {
// Are Notifications supported in the service worker?
if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
window.Demo.debug.log('Notifications aren\'t supported.');
test.textContent = test.textContent + ' Notifications not supported; ';
return;
}
test.textContent = test.textContent + ' initState; ';
// Check the current Notification permission.
// If its denied, it's a permanent block until the
// user changes the permission
if (Notification.permission === 'denied') {
window.Demo.debug.log('The user has blocked notifications.');
test.textContent = test.textContent + ' DENIED; ';
return;
}
// Check if push messaging is supported
if (!('PushManager' in window)) {
window.Demo.debug.log('Push messaging isn\'t supported.');
return;
}
// We need the service worker registration to check for a subscription
navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) {
// Do we already have a push message subscription?
serviceWorkerRegistration.pushManager.getSubscription()
.then(function (subscription) {
// Enable any UI which subscribes / unsubscribes from
// push messages.
var pushButton = document.querySelector('.js-push-button');
pushButton.disabled = false;
if (!subscription) {
// We aren’t subscribed to push, so set UI
// to allow the user to enable push
return;
}
// Keep your server in sync with the latest subscription
sendSubscriptionToServer(subscription);
// Set your UI to show they have subscribed for
// push messages
pushButton.textContent = 'Disable Push Messages';
isPushEnabled = true;
})
.catch(function (err) {
window.Demo.debug.log('Error during getSubscription()', err);
});
});
}
'use strict';
self.addEventListener('push', function (event) {
console.log('Received a push message', event);
var title = 'Yay a message.';
var body = 'We have received a push message.';
var icon = '/images/icon-192x192.png';
var tag = 'simple-push-demo-notification-tag';
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag
})
);
});
self.addEventListener('notificationclick', function (event) {
console.log('On notification click: ', event.notification.tag);
// Android doesn’t close the notification when you click on it
// See: http://crbug.com/463146
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
type: "window"
}).then(function (clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow('/');
}));
});
“严格使用”;
self.addEventListener('push',函数(事件){
log('收到推送消息',事件);
var title='是一条消息';
var body='我们已收到推送消息';
var icon='/images/icon-192x192.png';
var标记='简单推送演示通知标记';
event.waitill(
自我注册。展示通知(标题{
身体:身体,,
图标:图标,
标签:标签
})
);
});
self.addEventListener('notificationclick',函数(事件){
console.log('On notification click:',event.notification.tag);
//当你点击通知时,Android不会关闭它
//见:http://crbug.com/463146
event.notification.close();
//这将查看电流是否已打开,并且
//如果是的话
event.waitill(clients.matchAll({
类型:“窗口”
}).then(函数(客户端列表){
对于(var i=0;i
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="Sample illustrating the use of Push Messaing and Notifications.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Push Messaging & Notifications</title>
<!-- Include manifest file in the page -->
<link rel="manifest" href="manifest.json">
</head>
<body>
<h1>Push Messaging & Notifications</h1>
<p>Available in <a href="http://www.chromestatus.com/feature/5416033485586432">Chrome 42+</a> & <a href="http://www.chromestatus.com/feature/5480344312610816">Chrome 42+</a></p>
<p>To use this sample please do the following:</p>
<p>
<button class="js-push-button" disabled>
Enable Push Messages
</button>
</p>
<br />
<br />
<h2>cURL Command to Send Push</h2>
<div class="js-curl-command"></div>
<br />
<br />
<h3>Test</h3>
<div class="test"></div>
<script src="config.js"></script>
<script src="demo.js"></script>
<script src="main.js"></script>
</body>
</html>
推送信息&;通知
推送信息&;通知
可于&
要使用此示例,请执行以下操作:
启用推送消息
cURL命令发送Push
试验
那么我做错了什么
我使用的样本是完全错误的,还是我完全遗漏了什么
- 简言之;任何帮助都将不胜感激
或者尝试将
crossorigin=“use credentials”
添加到带有清单的
标记中。这是对带有auth cookies和auth头的请求清单的需要。所以这不是一个真正的答案,但在我修复了一些与代码无关的问题(您没有提供所有代码)后,它对我有效。我暂时把它放在这里:
您应该检查manifest.json
是否正确加载,我怀疑不是。检查Chrome开发工具以了解这一点。EDIT:从注释中:显然
manifest.json
加载了错误的MIME类型。这是在我的Web服务器(Nginx)中正确设置的,但在询问者的Web服务器(IIS)中没有正确设置。根据,它必须是application/manifest+json
,但在Chromeapplication/json
中也可以使用
此外,您还可以从mainfest.json
中删除一些内容。它不是a,因此像“浏览器操作”
、“清单版本”
和“权限”
这样的东西是不必要的。如果你想知道舱单上的内容,请阅读。这本不重要,但令人困惑
下次的建议:
- 使用标准的日志记录方式。我不知道什么是
,但是有window.Demo.debug
,console.log
和console.warn
console.error
- 最好下次把它放到网上某个地方,这样更容易调试。文件和出错的地方有着密切的关系。当你很容易看出问题所在时,你就更有可能得到答案
样式下添加
其中href
值是manifest.json文件的路径您的manifest.json
似乎不准确。请尝试更改它,然后再试一次。@B谢谢你的建议,但是我已经尝试过了,只是再试了一次-但它什么也没改变。由于你的指导,我添加了对gcm和存储的权限。缺少一些详细信息。这是Chrome应用程序/扩展还是普通的web应用程序?您在哪里调用serviceWorkerRegistration.pushManager.subscribe()
?(后者应该从页面中调用,而不是从服务人员中调用。)我遵循并让它工作。@ayke这是一个普通的web应用程序。我使用了与您相同的链接,因此我的serviceWorkerRegistration.pushManager.subscribe()
位于main.js中,与示例代码相同