【问题标题】:How can I initiate a PWA (progressive webapp) open from a click on a push notification?如何通过单击推送通知启动 PWA(渐进式 Web 应用程序)打开?
【发布时间】:2016-12-07 09:50:01
【问题描述】:

Following this example,我知道 PWA 如何打开 url,但我如何使用推送通知来启动应用程序本身?不在浏览器中,而是在全屏版 PWA 中。

【问题讨论】:

    标签: web-applications push-notification firebase-cloud-messaging progressive-web-apps


    【解决方案1】:

    这由浏览器控制。 此外,在决定打开一个窗口时,可能值得检查所需的目标 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 应用程序清单等)提供良好的用户体验。

    【讨论】:

    • 我已成功将应用添加到主屏幕并从主屏幕启动应用,但推送通知单击始终在浏览器模式下打开。有什么想法或建议吗?
    • 我唯一能想到的就是检查 chrome 版本,我假设你已经在你的网络应用清单中设置了独立?
    • 代码示例的链接是 404。
    【解决方案2】:

    取自 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("/");
          })
        );
    });
    

    【讨论】:

    • 谢谢,我刚试过这个应用程序。对于任何新消息,我都会收到两条通知,一条包含消息,另一条则说明网站已在后台更新。
    • 我收到一条通知“收到新聊天”,点击它会打开我之前在添加到主屏幕时安装的 PWA。请注意,如果您打开了 chrome 选项卡,它将聚焦该选项卡。
    • 当您收到“站点已在后台更新”时,这意味着“推送”事件代码中存在错误,即在将承诺传递给事件之前它们没有显示通知。等待解决。
    • 嗨,为什么它在新窗口中打开链接。是否可以在桌面上打开的同一个现有应用程序中打开,因为当我们点击通知时,它会打开另一个桌面应用程序并显示链接
    【解决方案3】:

    截至 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"
    }
    

    【讨论】:

    • 您好,您能否详细说明 start_url 在您说重要时所起的作用?
    【解决方案4】:

    找到了适合我的解决方案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('/');
          }
        })
      );
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多