【发布时间】:2016-11-21 17:00:24
【问题描述】:
我正在尝试制作云消息服务器,它将通知发送到 chrome(PC 和移动设备)。 我找到了https://developers.google.com/web/fundamentals/getting-started/codelabs/push-notifications/ 和教程。
在本教程中有一个监听器:
self.addEventListener('push', function(event) {
console.log('[Service Worker] Push Received.');
console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);
const title = 'Push Codelap';
const options = {
body: 'Yay it works.',
icon: 'images/icon.png',
badge: 'images/badge.png'
};
event.waitUntil(self.registration.showNotification(title, options));
});
它有效 - 当我通过 https://web-push-codelab.appspot.com/ 发送“Hello world”时,它会出现......但仅在 console.log 中。
如何更改选项以获取${event.data.text()} 而不是Yay it works?
我尝试将body: 'Yay it works.', 更改为body: ${event.data.text()},
但出现了Syntax Error: Unexpected token {。
【问题讨论】:
-
你需要使用字符串模板
${event.data.text()} -
@Hosar 这会导致语法错误:意外的令牌 {
标签: javascript google-cloud-messaging