简体   繁体   中英

Firebase notifications are sending successfully, but not showing up from my service worker

I tried to impliment a push notification system in my webapp. I am having two issues. I followed a tutorial , but updated it and registered a service worker, as I am not hosting via firebase.

  1. When I send a notification, my service worker doesnt respond to it.
  2. When I send a notification, only the first sent shows up.

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",
    projectId: "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);
    
});

generate-notification.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"}]}

Sorry guys, it was working, just not how I expected. I'll leave my question here, in hopes that my code might help someone else:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM