<tfoot id='GHngx'></tfoot>

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

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

        如果浏览器在后台,FCM 推送通知会到达两次

        时间:2023-08-02
        • <bdo id='PrEs2'></bdo><ul id='PrEs2'></ul>
          <legend id='PrEs2'><style id='PrEs2'><dir id='PrEs2'><q id='PrEs2'></q></dir></style></legend>

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

              <tbody id='PrEs2'></tbody>

            <tfoot id='PrEs2'></tfoot>

                  <i id='PrEs2'><tr id='PrEs2'><dt id='PrEs2'><q id='PrEs2'><span id='PrEs2'><b id='PrEs2'><form id='PrEs2'><ins id='PrEs2'></ins><ul id='PrEs2'></ul><sub id='PrEs2'></sub></form><legend id='PrEs2'></legend><bdo id='PrEs2'><pre id='PrEs2'><center id='PrEs2'></center></pre></bdo></b><th id='PrEs2'></th></span></q></dt></tr></i><div id='PrEs2'><tfoot id='PrEs2'></tfoot><dl id='PrEs2'><fieldset id='PrEs2'></fieldset></dl></div>
                  本文介绍了如果浏览器在后台,FCM 推送通知会到达两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经建立了一个简单的推送通知站点,如果浏览器在前台,通知就可以到达.

                  I've set up a simple push notification site, the notifications arrive okay if the browser is in foreground.

                  如果浏览器在后台,问题就开始了:通知到达两次,一次设置了图像样式和其他设置,另一次只有标题和正文消息.

                  The problem begins if the browser is in background: the notification arrives twice, one styled with image and other settings set and the other has only title and body message.

                  Service Worker 的内容:

                  Content of the service worker:

                  importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
                  importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');
                  
                  // Initialize the Firebase app in the service worker by passing in the
                  // messagingSenderId.
                  firebase.initializeApp({
                      'messagingSenderId': '...'
                  });
                  
                  const messaging = firebase.messaging();
                  
                  messaging.setBackgroundMessageHandler(function(payload) {
                      console.log('[firebase-messaging-sw.js] Received background message ', 
                      return null;
                  });
                  
                  self.addEventListener('install', function (event) {
                      event.waitUntil(skipWaiting());
                  });
                  
                  self.addEventListener('activate', function (event) {
                      event.waitUntil(clients.claim());
                  });
                  
                  self.addEventListener('push', function (event) {
                      var pushData = event.data.json();
                      try {
                          var notificationData = pushData.data;
                          notificationData.data = JSON.parse(notificationData.data);
                          console.log(notificationData);
                          self.registration.showNotification(pushData.notification.title, notificationData);
                      }
                      catch (err) {
                          console.log('Push error happened: ', err);
                      }
                  });
                  

                  客户端js:

                  firebase.initializeApp(firebaseConfig);
                  const messaging = firebase.messaging();
                  
                  messaging.onMessage(function (payload) {
                      console.log("notification recieved");
                      return null;
                  });
                  
                  self.addEventListener('push', function (event) {
                      console.log("window push stuff");
                      return null;
                  });
                  

                  谢谢!

                  推荐答案

                  messaging.setBackgroundMessageHandler 事件中加入这一行即可解决问题:

                  The problem can be solved with adding this line to the messaging.setBackgroundMessageHandler event:

                  self.registration.hideNotification();
                  

                  这样,默认通知将不会显示,您必须在 self.addEventListener 事件中显示您的通知.

                  This way, the default notification won't show and you have to show your notification in the self.addEventListener event.

                  这篇关于如果浏览器在后台,FCM 推送通知会到达两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用前台/焦点页面创建 Firebase 通知 下一篇:ReactJS - FCM 在 Safari 浏览器中不起作用

                  相关文章

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

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

                    <legend id='LWiST'><style id='LWiST'><dir id='LWiST'><q id='LWiST'></q></dir></style></legend>
                      • <bdo id='LWiST'></bdo><ul id='LWiST'></ul>
                    1. <tfoot id='LWiST'></tfoot>