起初为方便快速使用的是 alert ,后优化时发现并使用 notifications API 来代替。
使用 chrome.notifications API 须先配置 manifest 文件中的 permissions :
"permissions": [
"notifications",
"downloads",
"storage"
]
调用示例:
chrome.notifications.getPermissionLevel(function(level){
//获取用户是否为当前应用或应用启用通知(permissions中已配置可直接调用notifications)
if( level == 'granted' ){
//发出通知, type 默认为 basic
chrome.notifications.create(
'notify_alert1', // notifyId
{type: "basic", iconUrl: "icon-48.png", title: "更新完成!", message: "请查看页面数据是否已更新。"},
function(notifyId){
//不用移除该消息,否者不会显示
// chrome.notifications.clear(notifyId, function(){ });
}
);
}else{
//...
alert('更新完成!')
}
});