<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 通知

        时间:2023-08-02

          <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 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Firebase:更改服务人员的位置 下一篇:如果浏览器在后台,FCM 推送通知会到达两次

                  相关文章

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

                    <legend id='FyULF'><style id='FyULF'><dir id='FyULF'><q id='FyULF'></q></dir></style></legend>
                    1. <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>

                    2. <small id='FyULF'></small><noframes id='FyULF'>