【问题标题】:Chrome messaging - showing data instead of fixed messageChrome 消息 - 显示数据而不是固定消息
【发布时间】: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


【解决方案1】:

我昨天做的最好的事就是睡觉。新的想法和解决方案即将到来。

  const options = {
    body:  event.data.text(),
    icon: 'images/icon.png',
    badge: 'images/badge.png'
  };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    相关资源
    最近更新 更多