Chrome扩展程序-GCM通知引发错误但信息已存在?

3
我的谷歌浏览器扩展一直向这个函数抛出错误。
 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() {});
}

错误:

在运行 notifications.create 时出现未经检查的 runtime.lastError 错误:缺少某些必需的属性:类型、iconUrl、标题和消息。 位于 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.storage的chrome扩展程序进行了检查,并且它确实存在。

为什么错误是不正确的? :/

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

3
嗨 @新手。你解决了这个问题吗?我也遇到了同样的问题! - Nathan
1个回答

0

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

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接