【问题标题】:fcm onmessage not triggeredfcm onmessage 未触发
【发布时间】:2022-01-06 23:24:11
【问题描述】:

我看到这个问题被反复问过,我经历了很多帖子但未能在 9.6.1 中实现

我在 Windows 通知栏上收到通知。我需要的是在控制台中记录消息,然后对其执行一些操作。

需求:当消息使用token推送到客户端时 从服务器。该特定客户应接收数据并 使用我想显示自定义弹出窗口/模式的数据。这是 使用 FCM 可以实现吗?

下面是我在一个页面上配置的。

<script type="module">
        // Import the functions you need from the SDKs you need
        import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.1/firebase-app.js";
        import {getToken,onMessage} from "https://www.gstatic.com/firebasejs/9.6.1/firebase-messaging.js";
        import {getMessaging,onBackgroundMessage } from "https://www.gstatic.com/firebasejs/9.6.1/firebase-messaging-sw.js";
        console.log(Notification.permission);
        if (Notification.permission == 'granted') {
            loadFCM();
        }
        function loadFCM(p){
            console.log(p);
            // TODO: Add SDKs for Firebase products that you want to use
            // https://firebase.google.com/docs/web/setup#available-libraries
            // Your web app's Firebase configuration
            // For Firebase JS SDK v7.20.0 and later, measurementId is optional
            const firebaseConfig = {
            apiKey: "esdfdsgdsg",
            authDomain: "sdgsdgsdg",
            projectId: "sgsdgsdgds",
            storageBucket: "sgdsgdsgds",
            messagingSenderId: "sdgdsgsdg",
            appId: "1:sgdgs:web:sgdgdsg",
            measurementId: "G-sgdgdsdg"
            };
            // Initialize Firebase
            const app = initializeApp(firebaseConfig);
            const messaging = getMessaging(app);
            console.log(messaging);
            getToken(messaging, { vapidKey: 'sdgdsgsd-sdgdgsdggds' }).then((currentToken) => {
                if (currentToken) {
                    console.log('token to send...'+currentToken);
                } else {
                    // Show permission request UI
                    console.log('No registration token available. Request permission to generate one.');
                }
                }).catch((err) => {
                console.log('An error occurred while retrieving token. ', err);
            });
            onMessage(messaging, (payload) => {
                console.log('Message received. ', payload);
                messaging.setBackgroundMessageHandler(payload => {
                    console.log(payload);
                    const title = payload.data.title;
                    const options = {
                        body: payload.data.score
                    };
                    return self.registration.showNotification(title, options);
                });
            });
            onBackgroundMessage(messaging, (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'
                };
                self.registration.showNotification(notificationTitle, notificationOptions);
            });
            
        }

      </script>

我使用邮递员发送以下请求。请告诉我我在这里做错了什么。

  URL POST : https://fcm.googleapis.com/fcm/send
    {
        "data": {
            "title": "test",
            "body": "test",
        "click_action": "http://localhost:8001/"
    },
    "webpush": {
      "fcm_options": {
        "link": "https://domain.in"
      }
    },
    "notification":{  
    "title":"mytitle",
    "body":"mybody",
    "content_available": true   },
    "to": "dMNS-tergdrgd-PPMcqiCseoE3fLIQYAL9KbCxlnKcN2yQ1VV" }

【问题讨论】:

  • 请任何专家关注它。

标签: javascript firebase firebase-cloud-messaging web-notifications


【解决方案1】:

所以这里的问题是你的通知何时被正确安排/设计,即它看起来像这样:

{
    "notification": {  
        "title": "mytitle",
        "body": "mybody",
        "content_available": true 
    }  
}

它认为它是一个通知,并且永远不会调用 onMessage。 因此,请改为仅在 data 密钥中发送您的数据。

在邮递员中尝试发送这个:

URL POST : https://fcm.googleapis.com/fcm/send
{
    "data": {
        "title": "test",
        "body": "test",
        "click_action": "http://localhost:8001/"
    },
    "webpush": {
      "fcm_options": {
        "link": "https://domain.in"
      }
    },
    "to": "dMNS-tergdrgd-PPMcqiCseoE3fLIQYAL9KbCxlnKcN2yQ1VV" 
}

【讨论】:

  • 对不起,这并没有改变,无论如何我已经尝试过使用数据、webpush 以及后来添加的通知
  • 我的要求是,当我使用客户端的令牌从服务器发送通知时。那么我应该收到一些数据并使用我想在引导程序中显示弹出模式的数据,这可以使用 FCM 吗?
猜你喜欢
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
  • 2014-09-25
  • 2021-06-22
  • 2020-02-11
  • 1970-01-01
  • 2018-07-11
相关资源
最近更新 更多