[英]Is there a way to open mailto: and tel: links from a service worker?
我正在使用服务工作者处理的推送通知。
使用通知的主要动机是提供“呼叫”或“邮寄至”等操作,但我认为该行为与单击“邮寄:”链接相同。
它在 windows 上使用 chrome 工作(例如,单击时启动邮件应用程序),但在 android 上使用 chrome 失败,导致一个黑色标签,上面写着 url:...“mailto:...”
// Service Worker
self.addEventListener('notificationclick', function(event) {
const { contactRequest } = JSON.parse(event.notification.data);
switch(event.action) {
case 'call':
return clients.openWindow('tel:' + contactRequest.phone);
case 'mail':
return clients.openWindow('mailto:' + contactRequest.email);
}
});
更新了附加信息
这似乎是一个以多种方式表现出来的错误 - 声称在这里修复了一个不相关的起源 - https://bugs.chromium.org/p/chromium/issues/detail?id=792990
通知功能本身的选项并不多,您只能使用创建或使用 window 的openWindow
来执行其他选项。
MDN 也承认 Chrome 面临的问题 - https://developer.mozilla.org/en-US/docs/Web/API/Clients/openWindow
这里有两种方法
hash
作为单击通知的标记 - 这使您可以添加目标_top
或_blank
归功于MDN的伪代码
// Notification click event listener
self.addEventListener('notificationclick', e => {
// Close the notification popout
e.notification.close();
// Get all the Window clients
e.waitUntil(clients.matchAll({ type: 'window' }).then(clientsArr => {
// If a Window tab matching the targeted URL already exists, add a "#mailNotification"
const hadWindowToFocus = clientsArr.some(windowClient => windowClient.url === e.notification.data.url ? (windowClient.navigate(e.notification.data.url+"#mailNotification").then(function(client){client.focus()}), true) : false);
// Add additional code to add a
// Otherwise, open a new tab to the applicable URL and focus it.
if (!hadWindowToFocus) clients.openWindow(e.notification.data.url+"#mailNotification").then(windowClient => windowClient ?
windowClient.navigate(e.notification.data.url+"#mailNotification").then(function(client){client.focus()})
: null);
}));
// Then in your page, you can just use window.onhashChange event
window.onhashchange = weFixChromeLinks
function weFixChromeLinks () {
// ... Create an anchor tag that is formatted to your mailto has a target _top or _blank, hide the link and dispatch a click.
}
由于clients.openWindow
方法不提供针对_top
window 的能力,您可能必须设置支持_top
的中间页面 - 即
// Your intermediate page
window.open('mailto...','_top')
结束语 问题本身很丑- 浏览器应该知道意图,例如:应用程序、邮件程序等 - 似乎android的 Chrome 将其视为另一个 URL 并且惨遭失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.