<small id='g1b9Z'></small><noframes id='g1b9Z'>

      • <bdo id='g1b9Z'></bdo><ul id='g1b9Z'></ul>
    1. <tfoot id='g1b9Z'></tfoot>
      <i id='g1b9Z'><tr id='g1b9Z'><dt id='g1b9Z'><q id='g1b9Z'><span id='g1b9Z'><b id='g1b9Z'><form id='g1b9Z'><ins id='g1b9Z'></ins><ul id='g1b9Z'></ul><sub id='g1b9Z'></sub></form><legend id='g1b9Z'></legend><bdo id='g1b9Z'><pre id='g1b9Z'><center id='g1b9Z'></center></pre></bdo></b><th id='g1b9Z'></th></span></q></dt></tr></i><div id='g1b9Z'><tfoot id='g1b9Z'></tfoot><dl id='g1b9Z'><fieldset id='g1b9Z'></fieldset></dl></div>

        <legend id='g1b9Z'><style id='g1b9Z'><dir id='g1b9Z'><q id='g1b9Z'></q></dir></style></legend>

        使用前台/焦点页面创建 Firebase 通知

        Create Firebase notification with page in foreground/focus(使用前台/焦点页面创建 Firebase 通知)

          <bdo id='elpyh'></bdo><ul id='elpyh'></ul>

            <legend id='elpyh'><style id='elpyh'><dir id='elpyh'><q id='elpyh'></q></dir></style></legend>

                  <tbody id='elpyh'></tbody>
              • <small id='elpyh'></small><noframes id='elpyh'>

                <i id='elpyh'><tr id='elpyh'><dt id='elpyh'><q id='elpyh'><span id='elpyh'><b id='elpyh'><form id='elpyh'><ins id='elpyh'></ins><ul id='elpyh'></ul><sub id='elpyh'></sub></form><legend id='elpyh'></legend><bdo id='elpyh'><pre id='elpyh'><center id='elpyh'></center></pre></bdo></b><th id='elpyh'></th></span></q></dt></tr></i><div id='elpyh'><tfoot id='elpyh'></tfoot><dl id='elpyh'><fieldset id='elpyh'></fieldset></dl></div>
                  <tfoot id='elpyh'></tfoot>
                • 本文介绍了使用前台/焦点页面创建 Firebase 通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 Firebase Cloud Messaging(适用于网络),我如何生成在网页关闭或在后台显示的通知,但当我真正专注于网页时?

                  With Firebase Cloud Messaging (for web), how can I generate the notification that appears when the webpage is closed or in the background, but when I'm actually focused on the webpage?

                  据我了解,messaging.onMessage(...) 是我在页面处于焦点时处理传入消息的地方,但我似乎无法找到有关如何创建的文档通知弹出窗口就像页面在后台一样.

                  It's my understanding that messaging.onMessage(...) is where I handle incoming messages when the page is in focus, but I can't seem to find documentation on how I could still create the notification pop-ups as though the page were in the background.

                  感谢您的宝贵时间!

                  推荐答案

                  通过 Notification API 处理传入消息

                  handle incoming messges by Notification API

                  messaging.onMessage(function(payload) {
                      const notificationTitle = payload.notification.title;
                      const notificationOptions = {
                          body: payload.notification.body,
                          icon: payload.notification.icon,        
                      };
                  
                      if (!("Notification" in window)) {
                          console.log("This browser does not support system notifications");
                      }
                      // Let's check whether notification permissions have already been granted
                      else if (Notification.permission === "granted") {
                          // If it's okay let's create a notification
                          var notification = new Notification(notificationTitle,notificationOptions);
                          notification.onclick = function(event) {
                              event.preventDefault(); // prevent the browser from focusing the Notification's tab
                              window.open(payload.notification.click_action , '_blank');
                              notification.close();
                          }
                      }
                  });
                  

                  <小时>

                  通知已弃用.

                  向服务人员发送消息


                  Notification is deprecated.

                  send message to service worker

                     messaging.onMessage(function(payload) {
                          local_registration.active.postMessage(payload);
                       }
                  

                  接收消息并显示来自 sw.js 的推送

                  receive message and show push from sw.js

                  self.addEventListener('notificationclick', function(event) {
                  console.log('[firebase-messaging-sw.js] Received notificationclick event ', event);
                  
                  var click_action = event.notification.data;
                  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 == click_action  && 'focus' in client)
                              return client.focus();
                      }
                      if (clients.openWindow)
                          return clients.openWindow(click_action);
                      }));
                  
                  });
                  const showMessage = function(payload){
                      console.log('showMessage', payload);
                      const notificationTitle = payload.data.title;
                      const notificationOptions = {
                          body: payload.data.body,
                          icon: payload.data.icon,
                          image: payload.data.image,
                          click_action: payload.data.click_action,
                          data:payload.data.click_action
                      };  
                  
                  
                    return self.registration.showNotification(notificationTitle,notificationOptions); 
                  }   
                  messaging.setBackgroundMessageHandler(showMessage);
                  
                  self.addEventListener('message', function (evt) {     
                    console.log("self",self);
                    showMessage( evt.data );
                  })
                  

                  这篇关于使用前台/焦点页面创建 Firebase 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                  How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                  Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                  SQL Query DocumentDB in Azure Functions by an integer not working(通过整数在 Azure Functions 中 SQL 查询 DocumentDB 不起作用)
                  Azure Functions [JavaScript / Node.js] - HTTP call, good practices(Azure Functions [JavaScript/Node.js] - HTTP 调用,良好实践)
                  Azure Functions - Import Custom Node Module(Azure Functions - 导入自定义节点模块)

                    <tfoot id='FyULF'></tfoot>

                    <legend id='FyULF'><style id='FyULF'><dir id='FyULF'><q id='FyULF'></q></dir></style></legend>
                          <tbody id='FyULF'></tbody>

                        <i id='FyULF'><tr id='FyULF'><dt id='FyULF'><q id='FyULF'><span id='FyULF'><b id='FyULF'><form id='FyULF'><ins id='FyULF'></ins><ul id='FyULF'></ul><sub id='FyULF'></sub></form><legend id='FyULF'></legend><bdo id='FyULF'><pre id='FyULF'><center id='FyULF'></center></pre></bdo></b><th id='FyULF'></th></span></q></dt></tr></i><div id='FyULF'><tfoot id='FyULF'></tfoot><dl id='FyULF'><fieldset id='FyULF'></fieldset></dl></div>
                          <bdo id='FyULF'></bdo><ul id='FyULF'></ul>

                          • <small id='FyULF'></small><noframes id='FyULF'>