【发布时间】:2016-12-07 09:50:01
【问题描述】:
Following this example,我知道 PWA 如何打开 url,但我如何使用推送通知来启动应用程序本身?不在浏览器中,而是在全屏版 PWA 中。
【问题讨论】:
标签: web-applications push-notification firebase-cloud-messaging progressive-web-apps
Following this example,我知道 PWA 如何打开 url,但我如何使用推送通知来启动应用程序本身?不在浏览器中,而是在全屏版 PWA 中。
【问题讨论】:
标签: web-applications push-notification firebase-cloud-messaging progressive-web-apps
这由浏览器控制。 此外,在决定打开一个窗口时,可能值得检查所需的目标 URL 是否已在不同的选项卡/窗口中打开,以便您可以聚焦或打开它,视情况而定。检查这个book here for some code samples。
例如,在 Chrome 中,如果用户在其清单中添加了一个带有“独立”的网络应用程序到他们的主屏幕,那么当用户点击网络应用程序图标时,它会在没有 URL 栏的情况下打开。
当收到推送消息并打开同一个 Web 应用程序时,如果用户“最近”从主屏幕图标打开过 Web 应用程序(最近 10 天),则不会显示 URL 栏。如果用户在该时间段内未使用主页图标,则会打开通知点击并显示 URL 栏。
见for more info on this Chrome issue here:
具体:
网络应用程序必须添加到主屏幕,能够在 独立模式,并已从主屏幕中打开 最后十天。如果其中任何一个条件不满足, 通知将退回到现有行为。
还值得注意的是,PWA 与浏览器本身并不是正确的思考方式。您总是在浏览器中启动,只是在不同的模式下,例如“独立”与“浏览器”。
PWA(渐进式 Web 应用程序)在很大程度上是一个术语,用于描述使用一组 API 来通过新的 Web 技术(即服务工作者、推送、Web 应用程序清单等)提供良好的用户体验。
【讨论】:
取自 Jake Archibald 的 emojoy 演示:
self.addEventListener('notificationclick', event => {
const rootUrl = new URL('/', location).href;
event.notification.close();
// Enumerate windows, and call window.focus(), or open a new one.
event.waitUntil(
clients.matchAll().then(matchedClients => {
for (let client of matchedClients) {
if (client.url === rootUrl) {
return client.focus();
}
}
return clients.openWindow("/");
})
);
});
【讨论】:
截至 2018 年 10 月:
我使用 Chrome 69 管理它。
在本例中,它是一个子应用程序 (www.example.com/application)。
简而言之:仔细检查路径!
而且,当我从推送通知启动应用程序时,我也遇到了未加载 cookie(登录信息)的问题,它可以正常打开,但未登录。如果您关闭它并点击之前添加到主屏幕上的应用程序图标,应用启动已登录。
我使用以下方法完成了它(参见 cmets):
1) serviceworker.js
self.addEventListener('notificationclick', function (event)
{
//For root applications: just change "'./'" to "'/'"
//Very important having the last forward slash on "new URL('./', location)..."
const rootUrl = new URL('./', location).href;
event.notification.close();
event.waitUntil(
clients.matchAll().then(matchedClients =>
{
for (let client of matchedClients)
{
if (client.url.indexOf(rootUrl) >= 0)
{
return client.focus();
}
}
return clients.openWindow(rootUrl).then(function (client) { client.focus(); });
})
);
});
2) manifest.json
{
"short_name": "AppNickName",
"name": "ApplicationName",
"icons": [
{
"src": "./icon.png",
"sizes": "36x36",
"type": "image/png",
"density": 0.75
}
],
"start_url": "./", //This matters
"display": "standalone", //This also matters
"gcm_sender_id": "103953800507", //FCM always uses this number (April 2019)
"background_color": "#c8c8c8",
"theme_color": "#c8c8c8",
"orientation": "any"
}
【讨论】:
找到了适合我的解决方案here。
只需将此添加到您的服务人员:
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event.notification.tag);
// Android doesn't close the notification when you click on it
// See: http://crbug.com/463146
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(
clients.matchAll({
type: "window"
})
.then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow) {
return clients.openWindow('/');
}
})
);
});
【讨论】: