【发布时间】:2023-03-21 13:42:01
【问题描述】:
我在我的 MVC Web 应用程序上与 SignalR 一起使用通知。我正在从服务器的某个页面中调用客户端函数。问题是当该页面同时在两个浏览器选项卡中打开时,不会弹出任何通知。我应该提一下,当页面仅在一个选项卡中打开时,它确实可以正常工作。
我注意到,如果我在创建通知对象的行之前放置一个警报,两个选项卡都会收到通知,这就是我所期望的。提前致谢。
$(function () {
var notificationHub = $.connection.notificationHub;
$.connection.hub.start();
notificationHub.client.sendMessage = function (content) {
setTimeout(notifyMe(content), 10);
};
});
function notifyMe(message) {
if (!("Notification" in window)) {
alert("This browser does not support system notifications");
}
else if (Notification.permission === "granted") {
var notification = new Notification(message);
}
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification(message);
}
});
}
}
更新: 以下是通知创建代码前面有一个警报的场景,它可以按预期工作:
function notifyMe(message, system) {
if (!("Notification" in window)) {
alert("This browser does not support system notifications");
}
else if (Notification.permission === "granted") {
alert('hit');
var notification = new Notification(system, {
body: message,
tag: (Math.floor(Math.random() * 10)).toString(),
icon: 'Content/Images/logo.png',
dir: 'rtl',
background: '#ff0000'
});
}
else if (Notification.permission !== 'denied') {
alert('hit');
Notification.requestPermission(function (permission) {
if (permission === "granted") {
var notification = new Notification(message)
}
});
}
}
$(function () {
var notificationHub = $.connection.notificationHub;
$.connection.hub.start();
notificationHub.client.sendMessage = function (content, system) {
setTimeout(notifyMe(content, system), 10);
};
});
【问题讨论】:
标签: asp.net-mvc notifications signalr