微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Firebase 通知已成功发送,但未从我的 Service Worker 中显示

如何解决Firebase 通知已成功发送,但未从我的 Service Worker 中显示

我试图在我的 web 应用程序中实现推送通知系统。我有两个问题。我关注了 tutorial,但更新了它并注册一个 service worker,因为我不是通过 firebase 托管。

  1. 当我发送通知时,我的 Service Worker 没有响应。
  2. 当我发送通知时,只会显示一个发送的通知

index.js

var firebaseConfig = {
    apiKey: "XXXX",authDomain: "XXXX",projectId: "XXXX",storageBucket: "XXXX",messagingSenderId: "XXXX",appId: "XXXX"
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();

//test if push is supported by browser
if((('PushManager' in window)) && ('serviceWorker' in navigator)) {
    messaging.onMessage((payload) => {
        console.log('Message received. ',payload);
    });

    navigator.serviceWorker.register('firebase-messaging-sw.js').then((objServiceWorker) => { 
        console.log("service worker registered",objServiceWorker);
        messaging.useServiceWorker(objServiceWorker);
        messaging.requestPermission().then(function () {
            console.log("Notification permission granted.");

            // get the token in the form of promise
            return messaging.getToken();
        }).then(function(token) {
            //subscribe click
            ...save token to db

            //unsubscribe click
            ...delete token from db
        }).catch(function (err) {
            console.log("Unable to get permission to notify.",err);
            fallback();
        });
    }).catch(function(err) { 
        console.log('Service worker registration Failed,error:',err);
        fallback();
    }); 
} else {
    fallback();
}

firebase-messaging-sw.js

importScripts("https://www.gstatic.com/firebasejs/8.6.5/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/8.6.5/firebase-messaging.js");
var firebaseConfig = {
    messagingSenderId: "XXXX",apiKey: "XXXX",appId: "XXXX"
};
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
    console.log('[firebase-messaging-sw.js] Received background message ',payload);
    
});

生成通知.PHP

$arrHeaders = array(
    "Content-Type: application/json","Authorization: key=XXXX"
);
$arrPostFields = array(
    "notification" => array(
        "title" => "Notification Title","body" => "Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.","icon" => "https://www.whatever.com/images/symbol.svg","click_action" => "https://www.whatever.com/index.PHP?id=736",),"to" => $strRegistrationID,"priority" => "high"
);
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$strEndPoint);
curl_setopt($curl,CURLOPT_HTTPHEADER,$arrHeaders);
curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($arrPostFields));
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl,CURLOPT_POST,TRUE);
$arrOutput = curl_exec($curl);
curl_close($curl);

$arrOutput=

{"multicast_id":12345,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1234XXX"}]}

解决方法

抱歉,伙计们,它起作用了,只是出乎我的意料。我会在这里留下我的问题,希望我的代码可以帮助其他人:)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。