我正在尝试通过Chrome扩展程序后台页面发送通知,延迟时间为一分钟,但会立即发送通知。我正在使用的NotificationOption是" eventTime":
var tm = Date.now();
tm += 60000;
var opt = {
type: "basic",
title: "Homework Reminder",
message: "It worked!",
iconUrl: "images/asc.png",
eventTime: tm
};
chrome.notifications.create("", opt, function(id) {alert(new Date(tm));});
除了延迟,一切都有效。如何在将来发送通知?
答案 0 :(得分:1)
根据文件:
A timestamp associated with the notification, in milliseconds past the epoch (e.g. Date.now() + n).
所以我不认为这是为了将来通知。你应该使用常规的setTimeout:
setTimeout(function(){
var opt = {
type: "basic",
title: "Homework Reminder",
message: "It worked!",
iconUrl: "images/asc.png",
eventTime: tm
};
chrome.notifications.create("", opt, function(id) {alert(new Date(tm));});
}, 60000);
答案 1 :(得分:1)
要跟进Jim的评论,您需要使用chrome.alarms.create推迟创建通知。建议将警报事件处理程序添加到background.js文件(或manifest.json中为背景指定的任何脚本)。
例如(在background.js中):
// Add alarm listener from runtime onInstalled listener.
chrome.runtime.onInstalled.addListener(function() {
chrome.alarms.onAlarm.addListener(function(alarm) {
var opts = {};
opts.type = "basic";
opts.iconUrl = chrome.runtime.getURL("/images/logo.png");
opts.title = "Notification Title";
opts.message = "Message is here";
opts.eventTime = alarm.scheduledTime();
chrome.notifications.create(alarm.name, opts, function() {
console.log("Notification created", nopts);
});
});
});
您可能希望使用键入警报名称的chrome.storage为每个警报构建唯一的通知。