解决chrome扩展发通知不显示的问题

首先,要注意,使用这个api在background.js中,不是在content-script下。然后在content-script中要通过chrome.runtime.sendMessage 这个api来调起。

background.js中要监听sendMessage事件:

chrome.runtime.onMessage.addListener(
  (request, sender, sendResponse) => {
    if (request.contentScriptQuery === 'notification') {
      const {
        options
      } = request;
      chrome.notifications.create('notify1', options, (id) => {
        alert(JSON.stringify(chrome.runtime.lastError)); // 如果没调成功可以在这里看看报错,在生产环境别忘了注释掉
      });
    }
    console.log('Did not receive the response!!!');
  });

然后在你的content-script中这样发起:

const  options = {
  title: 'title',
  message: 'message',
  type: 'basic',
  iconUrl: 'img/icon-16.png',
}
chrome.runtime.sendMessage({
      contentScriptQuery: 'notification',
      options
  });

注意!!

iconUrl这个字段,必须为manifest中声明的图片文件,如果是网络图片可能还要在manifest中设置一下。

还不行看官方文档 ,搜关键字“required”,在“create“时有几个必填字段,必须都得写上。还要注意把manifest中的permission中添加”notifications“

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值