选项卡未聚焦时,不会触发Firebase onMessage

时间:2020-04-28 17:19:17

标签: javascript firebase firebase-cloud-messaging

我正在实施firebashe推送通知。当客户端选项卡被聚焦时,一切都可以正常工作,但是如果它不聚焦,我会弹出通知,但不会触发onMessage并且页面内容也不会刷新。

在通知中,我想更新我从服务器在Firebase响应的“数据” json组件中发送的一些文本。

正如我说的,如果标签集中,它可以很好地工作,但是如果不是,则我只是收到弹出通知并且这些文本没有更新

这是我的“ firebase-messaging-sw.js”

#include <iostream>
#include <cstdlib>
#include <map>
#include <memory>


class CachedThing
{
    private:
        CachedThing() = delete;
        CachedThing(int size) {m_size=size;}
        int m_size;
        static std::map<int, std::weak_ptr<CachedThing>>  m_cache;

    public:

    static std::shared_ptr<CachedThing> CreateFromCache(int size)
    {
        auto it = CachedThing::m_cache.find(size);
        if (it != CachedThing::m_cache.end())
        {
            std::cout << "FOUND  " << size << std::endl;
            return it->second.lock();
        }

        std::cout << "CREATE " << size << std::endl;

        auto CacheDeleter = [] (CachedThing *obj) {
            auto it=m_cache.find (obj->m_size);
            m_cache.erase ( it, m_cache.end() );  
            std::cout << "DELETE " << obj->m_size << std::endl;
            delete obj;
        };

        std::shared_ptr<CachedThing> f(new CachedThing(size), CacheDeleter);  
        m_cache[size]=f;
        return f;
    }
};

std::map<int, std::weak_ptr<CachedThing>> CachedThing::m_cache {};

int main()
{
    std::cout << "Here we go..." << std::endl;

    {
        auto f1 = CachedThing::CreateFromCache(18);
    }
    auto f3 = CachedThing::CreateFromCache(20);
    auto f2 = CachedThing::CreateFromCache(18);
    auto f4 = CachedThing::CreateFromCache(20);
    auto f5 = CachedThing::CreateFromCache(20);

}

这是我的init.js(用于初始化Firebase)

// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.14.2/firebase-messaging.js');

var firebaseConfig = {
    apiKey: "*******************",
    authDomain: "*******************",
    databaseURL: "*******************",
    projectId: "*******************",
    storageBucket: "*******************-*******************.*******************.com",
    messagingSenderId: "*******************",
    appId: "1:*******************:*******************"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
    console.log('[firebase-messaging-sw.js] Received background message ', payload);
    // Customize notification here
    const notificationTitle = 'Background Message Title';
    const notificationOptions = {
        body: 'Background Message body.',
        icon: '/firebase-logo.png'
    };
    return self.registration.showNotification(notificationTitle,
        notificationOptions);

});

当然,我在

之前加入了Firebase库
var firebaseConfig = {
    apiKey: "*******************",
    authDomain: "*******************",
    databaseURL: "*******************",
    projectId: "*******************",
    storageBucket: "*******************-*******************.*******************.com",
    messagingSenderId: "*******************",
    appId: "1:*******************:*******************"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();

messaging.usePublicVapidKey('********************************************');

这是我的页面js(具有“ onMessage”功能)

    <script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-messaging.js"></script>

我希望你能帮助我 非常感谢您,我的英语不好。

0 个答案:

没有答案