Javascript 在meteor项目中使用chrome推送通知mainfest.json
我正在遵循一个关于如何实现的指南,并且我正在尝试将它作为一个包在Meteor应用程序中实现 因为我无法包含manifest.json,所以我得到了“注册失败-未提供发件人id”或“注册失败-权限被拒绝”。那么,如何将此文件包括在我的项目中 manifest.json如下所示:Javascript 在meteor项目中使用chrome推送通知mainfest.json,javascript,meteor,google-cloud-messaging,Javascript,Meteor,Google Cloud Messaging,我正在遵循一个关于如何实现的指南,并且我正在尝试将它作为一个包在Meteor应用程序中实现 因为我无法包含manifest.json,所以我得到了“注册失败-未提供发件人id”或“注册失败-权限被拒绝”。那么,如何将此文件包括在我的项目中 manifest.json如下所示: { "permissions": [ "gcm" ], "name": "push", "short_name": "push notification", "display": "standalone",
{
"permissions": [ "gcm" ],
"name": "push",
"short_name": "push notification",
"display": "standalone",
"gcm_sender_id": "0000000000000"
}
self.addEventListener('push', showNotification)
self.addEventListener('notificationclick', closeNotificationAndOpenWindow)
function showNotification(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
})
)
}
function closeNotificationAndOpenWindow(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('/')
}))
}
我尝试将其包含在package.js中,如:
api.addAssets('manifest.json', 'client');
还可以将所需变量(gcm_sender_id)放入settings.json中,并使用meteor--settings settings.json启动meteor,但没有任何效果
我的服务人员注册从调用Cpn.serviceWorkerRegistration
开始:
Cpn = {};
Cpn.serviceWorkerRegistration = function () {
console.log("serviceWorkerRegistration called");
subscribe();
console.log(navigator);
// Check that service workers are supported, if so, progressively
// enhance and add push messaging support, otherwise continue without it.
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js')
.then(initialiseState);
} else {
console.warn('Service workers aren\'t supported in this browser.');
}
}
// Once the service worker is registered set the initial state
initialiseState = function () {
console.log("initialiseState");
// Are Notifications supported in the service worker?
if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
console.warn('Notifications aren\'t supported.');
return;
}
// Check the current Notification permission.
// If its denied, it's a permanent block until the
// user changes the permission
if (Notification.permission === 'denied') {
console.warn('The user has blocked notifications.');
return;
}
// Check if push messaging is supported
if (!('PushManager' in window)) {
console.warn('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) {
if (!subscription) {
return;
}
// Keep your server in sync with the latest subscriptionId
sendSubscriptionToServer(subscription);
})
.catch(function (err) {
console.warn('Error during getSubscription()', err);
});
});
}
function subscribe() {
navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.subscribe()
.then(function (subscription) {
// The subscription was successful
// TODO: Send the subscription.endpoint to your server
// and save it to send a push message at a later date
return sendSubscriptionToServer(subscription);
})
.catch(function (e) {
if (Notification.permission === 'denied') {
// The user denied the notification permission which
// means we failed to subscribe and the user will need
// to manually change the notification permission to
// subscribe to push messages
console.warn('Permission for Notifications was denied');
} else {
// A problem occurred with the subscription; common reasons
// include network errors, and lacking gcm_sender_id and/or
// gcm_user_visible_only in the manifest.
console.error('Unable to subscribe to push.', e);
}
});
});
}
服务人员如下所示:
{
"permissions": [ "gcm" ],
"name": "push",
"short_name": "push notification",
"display": "standalone",
"gcm_sender_id": "0000000000000"
}
self.addEventListener('push', showNotification)
self.addEventListener('notificationclick', closeNotificationAndOpenWindow)
function showNotification(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
})
)
}
function closeNotificationAndOpenWindow(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',showNotification)
self.addEventListener('notificationclick',closeNotificationAndOpenWindow)
函数showNotification(事件){
console.log('收到推送消息',事件)
var title='Yay a message'
var body='我们已收到推送消息。'
var icon='/images/icon-192x192.png'
var标记='简单推送演示通知标记'
event.waitill(
自我注册。展示通知(标题{
身体:身体,,
图标:图标,
标签:标签
})
)
}
函数closeNotificationAndOpenWindow(事件){
console.log('通知时单击:',event.notification.tag)
//当你点击通知时,Android不会关闭它
//见:http://crbug.com/463146
event.notification.close()
//这将查看电流是否已打开,并且
//如果是的话
event.waitill(clients.matchAll({
类型:“窗口”
}).then(函数(客户端列表){
对于(var i=0;i
请提供一些显示问题的代码。我将manifest.json
放在公用文件夹中,并将
添加到head标签中,突然间它起了作用。我以前试过这个,但没有任何结果。我不知道为什么它现在可以工作,可能是因为JSON文件编码选项?您是否已设法与工作人员建立推送通知服务?是的,我将service worker.js
和manifest.JSON
放在公用文件夹中,然后我可以从我的包订阅推送通知。我不需要将这些文件中的任何一个添加到我的包package.js
。请提供一些显示问题的代码。我将manifest.json
放在公用文件夹中,并在head标签中添加了
,然后它突然起了作用。我以前试过这个,但没有任何结果。我不知道为什么它现在可以工作,可能是因为JSON文件编码选项?您是否已设法与工作人员建立推送通知服务?是的,我将service worker.js
和manifest.JSON
放在公用文件夹中,然后我可以从我的包订阅推送通知。我不需要将这些文件中的任何一个添加到我的包package.js
。也见我的评论上面。