Push notification 多个Chrome推送通知自动关闭,最后一个除外
我有一个从服务器获取多个通知的服务工作者。问题是除了最后一个通知外,Chrome中的所有通知都会自动关闭。我做错了什么Push notification 多个Chrome推送通知自动关闭,最后一个除外,push-notification,service-worker,web-push,push-api,Push Notification,Service Worker,Web Push,Push Api,我有一个从服务器获取多个通知的服务工作者。问题是除了最后一个通知外,Chrome中的所有通知都会自动关闭。我做错了什么 self.addEventListener('push', function(event) { var subscriptionId; var sessionId; var notification = {}; event.waitUntil( self.registration.pushManager.getSubscription().then(function(
self.addEventListener('push', function(event) {
var subscriptionId;
var sessionId;
var notification = {};
event.waitUntil(
self.registration.pushManager.getSubscription().then(function(subscription) {
subscriptionId = subscription.endpoint.split('/');
subscriptionId = subscriptionId[subscriptionId.length - 1];
notification.title = 'Yay a message.';
notification.icon = '/app/img/icon-256x256.png';
notification.tag = 'notification-tag-' + (new Date)*1;
notification.messages = [];
//get context
fetch(requestUrl,
{
method: 'post',
body: body
}).then(function(response) {
response.json().then(function(data) {
sessionId = response.headers.get('SessionId');
fetch(requestUrl + '?SessionId=' + sessionId, {
method: 'post',
headers: JSON.stringify({
'Content-Type': 'application/json'
}),
body: JSON.stringify({
data: {
subscriberId: subscriptionId
}
})
}).then(function(responce) {
responce.json().then(function(data) {
data = data.data;
if (!data.chromeNotifications || !data.chromeNotifications.length) return;
data.chromeNotifications.forEach(function(push) {
notification.messages.push(push.message);
});
}).then(function() {
var promises = [];
for(var i=0; notification.messages && i<notification.messages.length; i++) {
promises.push(self.registration.showNotification(notification.title, {
body: notification.messages[i],
icon: notification.icon,
tag: notification.tag
}));
}
return Promise.all( promises );
});
});
})
});
}).catch(function(error) {
console.error('Unable to get subscription data', error);
var title = 'An error occurred';
var message = 'We were unable to get the information for this push message';
var notificationTag = 'notification-error';
return self.registration.showNotification(title, {
body: message,
tag: notificationTag
});
})
);
self.addEventListener('push',函数(事件){
变量下标ID;
无柄变异体;
var通知={};
event.waitill(
self.registration.pushManager.getSubscription().then(函数(订阅){
subscriptionId=subscription.endpoint.split('/');
subscriptionId=subscriptionId[subscriptionId.length-1];
notification.title='Yay a message';
notification.icon='/app/img/icon-256x256.png';
notification.tag='notification tag-'+(新日期)*1;
notification.messages=[];
//获取上下文
获取(请求URL,
{
方法:“post”,
身体:身体
}).然后(功能(响应){
response.json().then(函数(数据){
sessionId=response.headers.get('sessionId');
获取(requestUrl+'?SessionId='+SessionId{
方法:“post”,
标题:JSON.stringify({
“内容类型”:“应用程序/json”
}),
正文:JSON.stringify({
数据:{
subscriptionId:subscriptionId
}
})
}).然后(函数(响应){
response.json().then(函数(数据){
data=data.data;
如果(!data.chromeNotifications | |!data.chromeNotifications.length)返回;
数据.色度化.forEach(功能(推送){
通知.messages.push(push.message);
});
}).然后(函数(){
var承诺=[];
对于(var i=0;notification.messages&&i,在从服务器获取挂起的通知之前,在示例的第13行,您将标记
设置为传入推送消息的唯一标记一次。然后,该标记
将用于从服务器获取的每个消息,在第47行
通知的标记
可用于指示在创建通知时,在显示全新的标记之前,通知应尝试替换共享相同标记的任何以前的通知。在这种情况下,您会重复覆盖以前的通知
解决方案是根本不使用标记
,因为您希望它无论如何都是唯一的。保留它作为错误通知是有意义的——如果您将它显示两次,这对用户没有帮助。就在从服务器获取挂起的通知之前,在示例的第13行,您将标记
设置一次,使其对于t是唯一的接收推送消息。然后,在第47行,此标记将用于从服务器获取的每条消息
通知的标记
可用于指示在创建通知时,在显示全新的标记之前,通知应尝试替换共享相同标记的任何以前的通知。在这种情况下,您会重复覆盖以前的通知
解决方案是根本不使用标记
,因为您希望它无论如何都是唯一的。保留它作为错误通知是有意义的——如果您将它显示两次,对用户没有任何帮助