JavaScript FCM Web推送通知click_action不起作用

[更新]

现在我正在使用this official code,它可以用于推送通知,但是关于click_action的相同操作仍然无法正常工作


我正在从控制台发送带有点击动作参数设置的测试通知

JavaScript FCM Web推送通知click_action不起作用

我有这个HTML代码

<div id="token"></div>
<div id="msg"></div>
<div id="notis"></div>
<div id="err"></div>
<script src="https://www.gstatic.com/firebasejs/7.2.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.2.1/firebase-messaging.js"></script>
<!-- For an optimal experience using Cloud Messaging,also add the Firebase SDK for Analytics. -->
<script src="https://www.gstatic.com/firebasejs/7.2.1/firebase-analytics.js"></script>
<script>
    MsgElem = document.getElementById("msg");
    TokenElem = document.getElementById("token");
    NotisElem = document.getElementById("notis");
    ErrElem = document.getElementById("err");
    // Initialize Firebase
    // TODO: Replace with your project's customized code snippet
    var config = {
        apiKey: "AIz**************yKA",authDomain: "DOMAIN.firebaseapp.com",databaseURL: "https://URL.firebaseio.com",projectId: "PROJECT_ID",storageBucket: "BUCKET_ID.appspot.com",messagingSenderId: "SENDER_ID",appId: "1********************6"
    };
    firebase.initializeApp(config);

    const messaging = firebase.messaging();
    messaging
        .requestPermission()
        .then(function () {
            MsgElem.innerHTML = "Notification permission granted." 
            console.log("Notification permission granted.");

            // get the token in the form of promise
            return messaging.getToken()
        })
        .then(function(token) {
            TokenElem.innerHTML = "token is : " + token
        })
        .catch(function (err) {
            ErrElem.innerHTML =  ErrElem.innerHTML + "; " + err
            console.log("Unable to get permission to notify.",err);
        });

    messaging.onmessage(function(payload) {
        console.log("Message received. ",payload);
        NotisElem.innerHTML = NotisElem.innerHTML + JSON.stringify(payload);
        //kenng - foreground notifications
        const {title,...options} = payload.notification;
        navigator.serviceWorker.ready.then(registration => {
            registration.showNotification(title,options);
        });
    });
</script>

firebase-messaging-sw.js

importScripts('https://www.gstatic.com/firebasejs/7.2.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.2.1/firebase-messaging.js');
// For an optimal experience using Cloud Messaging,also add the Firebase SDK for Analytics. 
importScripts('https://www.gstatic.com/firebasejs/7.2.1/firebase-analytics.js');

// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
  'messagingSenderId': 'SENDER_ID'
});

// 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: '/itwonders-web-logo.png'
  };

  return self.registration.showNotification(notificationTitle,notificationOptions);
});

我收到通知,但是单击它会在新标签中打开网站的相同域,但不会打开click_action参数的值

lipton_12345678 回答:JavaScript FCM Web推送通知click_action不起作用

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3018642.html

大家都在问