45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import IWHNotify from "../../interface/IWHNotify";
|
|
import Log from "../Log";
|
|
import WuhuBase from "../WuhuBase";
|
|
|
|
export default class NotificationUtils extends WuhuBase {
|
|
className = 'NotificationUtils';
|
|
private permission: boolean = window.Notification && window.Notification.permission === 'granted';
|
|
|
|
public push(msg: string, options: IWHNotify = {}): void {
|
|
let { notifies } = NotificationUtils.glob;
|
|
|
|
if (options.sysNotify && this.permission) {
|
|
let tmpNode = document.createElement('p');
|
|
tmpNode.innerHTML = msg;
|
|
let notify = new Notification('芜湖助手', {
|
|
body: Log.getTime() + '\r\n' + tmpNode.innerText,
|
|
// requireInteraction: true,
|
|
// renotify: true,
|
|
// tag: '芜湖助手' + Utils.getRandomInt(0, 99),
|
|
});
|
|
let id = notifies.count++;
|
|
notifies[id] = notify;
|
|
notify.addEventListener(
|
|
'close',
|
|
() => {
|
|
notifies[id] = null;
|
|
}
|
|
);
|
|
notify.addEventListener(
|
|
'click',
|
|
() => {
|
|
options.sysNotifyClick ? options.sysNotifyClick() : null;
|
|
window.focus();
|
|
}
|
|
);
|
|
notify.addEventListener(
|
|
'show',
|
|
() => {
|
|
// setTimeout(() => notify.close(), (options.timeout || 3) * 1000);
|
|
Log.info('通知id: ', id)
|
|
}
|
|
);
|
|
}
|
|
}
|
|
} |