javascript - 点击推送通知不会每次都打开新的网址
问题描述
我想每次点击 javascript 中的通知时打开一个新 URL。使用以下代码,我可以接收推送通知。我可以重定向到我第一次发送的 URL。但是第二次我无法重定向到新的 URL,而是重定向到我之前传递的 URL。这是我的代码,请帮助我。
const messaging = firebase.messaging()
messaging.setBackgroundMessageHandler(function (payload) {
console.log('[firebase-messaging-sw.js] Received background message', payload.data);
const notification = payload.data;
const notificationTitle = notification.title;
const notificationOptions = {
body: notification.message,
icon: notification.icon || "/images/icon.png"
};
self.addEventListener('notificationclick', function(event) {
event.notification.close();
event.waitUntil(self.clients.openWindow(notification.url));
});
return self.registration.showNotification(notificationTitle, notificationOptions);
});
解决方案
我得到了它的解决方案,我创建了全局变量,每次setBackgroundMessageHandler
函数调用我都更新它
var myUrl = "";
const messaging = firebase.messaging()
messaging.setBackgroundMessageHandler(function (payload) {
console.log('[firebase-messaging-sw.js] Received background message', payload.data);
const notification = payload.data;
const notificationTitle = notification.title;
const notificationOptions = {
body: notification.message,
icon: notification.icon || "/images/icon.png"
};
myUrl = notification.url;
self.addEventListener('notificationclick', function(event) {
event.waitUntil(self.clients.openWindow(myUrl));
event.notification.close();
});
return self.registration.showNotification(notificationTitle, notificationOptions);
});
推荐阅读
- vba - 无法通过 VBA 中的字符串值访问 excel 数据透视表字段
- javascript - ng-if 没有根据变量更新
- chef-infra - 刀:找不到命令
- python - 从列表中创建多个复选框并获取所有值
- powershell - PowerShell 脚本不会按顺序运行
- grails - com.nimbusds.jose.JOSEException:不支持的 JWS 算法 RS256,在使用 Auth0 令牌时必须是 HS256、HS384 或 HS512
- jquery - jQuery将单独的对象水平滚动在一起
- powershell - 在 Powershell 中将用户移动到禁用的 OU
- swift - 获取base64Encoded图像的真实尺寸
- postgresql - 如何从 csv 复制到 postgresql 表并忽略没有标题的列