javascript - Chrome 扩展 - GCM 通知抛出错误但信息存在吗?

标签 javascript google-chrome-extension promise

我的 chrome 扩展一直向这个函数抛出错误

 function messageReceived(message) {
  // A message is an object with a data property that
  // consists of key-value pairs.

  // Pop up a notification to show the GCM message.
  chrome.notifications.create(getNotificationId(), {
    title: message.data.name,
    iconUrl: 'assets/img/cat.jpg',
    type: 'basic',
    message: message.data.prompt,
    buttons : [
    { title: "Accept" },
    { title: "Reject" }
    ]
  }, function() {});
}

错误:

Unchecked runtime.lastError while running notifications.create: Some of the required properties are missing: type, iconUrl, title and message. at messageReceived

然而,这些都是真实存在的。每次我添加以下函数时都会出现错误

function notificationBtnClicked(notification, ibtn) {
  console.log(notification)
  console.log(ibtn)

  if (ibtn==0) {
    chrome.storage.local.get("name", function(name){
      chrome.storage.local.get("email",function(email){
          //call other users
          var email = email
          var name = name
          $.ajax({
               url: 'some api',
               data:'{email:email, name:name}',
               ajax:true,
               success: function(result)
               {
                 alert(result);
               }
             });
      });
    })

  }else {
    //snooze
  }
}

但是,我不明白这是什么问题。我通过下载一些 chrome 扩展程序检查了 chrome.storage,您可以在其中查看它。

为什么错误不正确? :/

chrome.gcm.onMessage.addListener(messageReceived);
chrome.notifications.onButtonClicked.addListener(notificationBtnClicked);

最佳答案

确保所有必需的通知属性都有它们的值。您使用的“iconUrl”必须是数据 URL、blob URL 或与 'notification.create' 所需的扩展程序的 .crx 文件中的资源相关的 URL。方法。请注意您使用的 Chrome 版本。

此外,包括回调并检查 chrome.runtime.lastError

function callback() {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
} else {
// Tab exists
}
}

关于javascript - Chrome 扩展 - GCM 通知抛出错误但信息存在吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35677913/

相关文章:

javascript - js promise 问题 (bluebird)

javascript - d3.timer() 帧 - 减慢速度

javascript - 将下一页上的按钮设置为像用户单击的按钮一样聚焦

javascript - chrome extension api是否支持直接从浏览缓存下载?

javascript - Chrome 扩展程序 - 单击扩展程序、新选项卡和重定向

google-chrome - 如何在 Chrome 网上应用店跟踪用户和安装情况?

javascript - NodeJS 将 Promise 转换为 Promise.all

javascript - Canvas.js 图表不更新新数据

javascript - ReactJS:意外 token '<'

exception - Dojo - 吞下 Promise 中的 ReferenceError 异常