Obtinere token in php firebase fcm

Incerc sa adaug notificari in site-ul meu cu firebase
Acum incerc sa salvez token-ul in baza de date mysql

                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);
                        });

Cum pot obtine acest token si in php pentru a putea salva in baza mea de date?

Poate gresesc, dar aici l-ai putea trimite cu un POST. :slight_smile:

La o prima vedere.

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
                sendToken(token)
        })
        .catch(function (err) {
                ErrElem.innerHTML =  ErrElem.innerHTML + "; " + err
                console.log("Unable to get permission to notify.", err);
        });


function sendToken(token) {
    return fetch(url, {

        method: "post",
        //mode: "cors",
        //cache: "no-cache",
        //credentials: "same-origin",
        headers: {
            'Content-Type': 'application/json'
        },
        //redirect: "follow",
        //referrer: "no-referrer",
        body: JSON.stringify(token),

    })
    .then(response => response.json())
        .then(data => console.log(data))
        .catch(err => console.error(err));

}

Vezi daca merge. Poate va fi nevoie sa adaptezi codul la nevoile tale.
Fetch API are mai multe optiuni. Poate nu ai nevoie de toate

Sau cu Axios in loc de Fetch.
Spor!

1 Like

Recomand sa folosesti axios si sa trimiti un request de tip POST cu ce ai nevoie catre endpointul tau php.
axios.post(url, data).then(....

Am incercat

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
                sendToken(token)
        })
        .catch(function (err) {
                ErrElem.innerHTML =  ErrElem.innerHTML + "; " + err
                console.log("Unable to get permission to notify.", err);
        });


function sendToken(token) {
    return fetch(includes/class-user.php/firebase($token), {

        method: "post",
        //mode: "cors",
        //cache: "no-cache",
        //credentials: "same-origin",
        headers: {
            'Content-Type': 'application/json'
        },
        //redirect: "follow",
        //referrer: "no-referrer",
        body: JSON.stringify(token),

    })
    .then(response => response.json())
        .then(data => console.log(data))
        .catch(err => console.error(err));

}
    public function firebase($token, $return_info = false) {
        global $db;
        $query = $db->query(sprintf("UPDATE users where SET firebase = %s", secure($token) )) or _error("SQL_ERROR_THROWEN");
        if($query->num_rows > 0) {
            if($return_info) {
                $info = $query->fetch_assoc();
                return $info;
            }
            return true;
        }
        return false;
    }	

Rezultatul este o eroare in consola “Uncaught SyntaxError: Unexpected token ‘-’”
la linia return fetch(includes/class-user.php/firebase($token), {

Asta pt ca aici trebuie url-ul site-ului tau. Adresa web, nu import de clasa php sau ce este

return fetch("http://my_url.com/ceva")...

Daca este local site-ul, o sa fie cu localhost

1 Like

Am pus

function sendToken(token) {
    return fetch($_SERVER['SERVER_NAME']/firebase($token), {

        method: "post",
        //mode: "cors",
        //cache: "no-cache",
        //credentials: "same-origin",
        headers: {
            'Content-Type': 'application/json'
        },
        //redirect: "follow",
        //referrer: "no-referrer",
        body: JSON.stringify(token),

    })
    .then(response => response.json())
        .then(data => console.log(data))
        .catch(err => console.error(err));

}

si acum primesc eroarea asta
Uncaught SyntaxError: missing ) after argument list

Ti-as sugera sa citesti putin iainte de a copia codul.
https://www.w3schools.com/tags/ref_httpmethods.asp

Prin url ma refer la asa ceva

am pus
https://domeniu.com/includes/class-user.php/firebase($token)

in class-user.php

am adaugat

    public function firebase($token, $return_info = false) {
        global $db;
        $query = $db->query(sprintf("UPDATE users where SET firebase = %s", secure($token) )) or _error("SQL_ERROR_THROWEN");
        if($query->num_rows > 0) {
            if($return_info) {
                $info = $query->fetch_assoc();
                return $info;
            }
            return true;
        }
        return false;
    }