Italian Ricezione per nik name con Firebase(Notifiche)

ken87

Active Member
Licensed User
Longtime User
Ciao ho una mia app , e una pagina web dove c'è un box in cui è possibile inviare una notifica ad un utenza ad esempio Marco.
la mia pagina web che sta in un mio dominio personale dovrebbe inviare la notifica al server google Firebase all'utanza Marco.
Il dominio Firewebase invia il messaggio alla mia app. Ho visto che nel panello di configurazione c'è un campo
Nikname app serve per fare il filtro con le mie utenze?
Posso scrivere:
B4X:
dim niknamefirebase as string
If Nicknamefirebase = utenza_login_app then
invia_notifica
end if
e in che modo posso inviare il messaggio a Firebase ?
Immagino che il box che invia il nik e il messaggio a una pagina php questo chiama il servizio google.
Poi la notifica se ha fatto l'acesso Marco arriva se invece c'è loggata Marta viene rifiutata.
Potete darmi una mano? Altra cosa riccordo che Firebase ha un limite di notifiche , si intende per l'invio multiplo nello stesso momento giusto o giornaliero.. e se quante sono?
Grazie mille
 

Attachments

  • fire.jpg
    fire.jpg
    56 KB · Views: 258
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
Ciao

FCM Notification sono liberi e illimitati
Il messaggio dovrà essere contenuto nei 4096 caratteri (4k documentato)

Le notifiche possono essere inviati a tutti ( general ) a gruppi ( general_gruppo1 ) o a utente ( general_12 )
Nel servizio FireBaseMessaging inserisci per ricevere:
B4X:
...
Public Sub SubscribeToTopics
    Log("*******************" & " general_" & idpizzeria)
    fm.SubscribeToTopic("general_" & id ) 'Può essere un gruppo o un utente
End Sub
....

Relativamente alla parte server ( non sappiamo come sia scritto , quindi ti riporto esempio in php ) potresti utilizzare il seguente codice:

B4X:
<?php
    $url = "https://fcm.googleapis.com/fcm/send";
    $token = "general_12";
    $serverKey = 'your server token of FCM project';
    $title = "Notification title";
    $body = "Hello I am from Your php server";
    $notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
    $arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
    $json = json_encode($arrayToSend);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key='. $serverKey;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    //Send the request
    $response = curl_exec($ch);
    //Close request
    if ($response === FALSE) {
    die('FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);
?>
 

ken87

Active Member
Licensed User
Longtime User
Ciao quando tento l'invio ricevo questo errore
B4X:
{"multicast_id":6574463376353790383,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
Ho letto che potrebbe essere la chiave ma l'ho provata prima l'invio tramite app e li va bene..
Cosa può essere?
Il file google-services.json va copiato nella cartella dove sta il file php?
 

MarcoRome

Expert
Licensed User
Longtime User
La chiave è del Server no dell'App. Devi creare la chiave per il server.
 

ken87

Active Member
Licensed User
Longtime User
Ok,dove la trovo la chiave del server da scrivere nel file PHP .
 

MarcoRome

Expert
Licensed User
Longtime User
Stesso percorso di dove prendi la chiave per Android...

upload_2019-1-18_18-25-39.png


oppure fai una piccola ricerca su google tipo "get key firebase notification for web"
 

ken87

Active Member
Licensed User
Longtime User
Ciao ho incluso la chiave del server vedi immagine:
ma mi da sempre lo stesso errore:
Includo anche il sorgente
B4X:
<?php
    $url = "https://fcm.googleapis.com/fcm/send";
    $token = "general";
    $serverKey = "AAAA9JyC3ek:APA91bH-dRj3dobtUBRsLsDkOJoNaT1iY9Fv-5iweF6uIDoHokCKPwAv8dWlX2TIDgxu0i_LNU-VoOjPMg-wllJiRU65kn5lIWjhyK0xTTpOPSct42M_ETn65-H-95qkwaNVxxxxxx";
    $title = "Notification title";
    $body = "Hello I am from Your php server";
    $notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
    $arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
    $json = json_encode($arrayToSend);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key='. $serverKey;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    //Send the request
    $response = curl_exec($ch);
    //Close request
    if ($response === FALSE) {
    die('FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);
?>
?>

Ho oscurato le ultime cifre con xxx
 

Attachments

  • chiave.jpg
    chiave.jpg
    60.4 KB · Views: 219

MarcoRome

Expert
Licensed User
Longtime User
Per comodità anche di altri riporta come hai fatto con un esempio. Grazie
 

ken87

Active Member
Licensed User
Longtime User
Ciao ho usato il php che mi hai passato tu:
B4X:
<?php
        $url = "https://fcm.googleapis.com/fcm/send";
        $topico = "general";
        $api_key = "AAAA9Jyxxxxxxxxxx"; //key server personale
        $titulo = "Prima Notifica";
        $corpo = "Messaggio n 1";
        $legenda = "Notifica";
 

            $headers = array
            (
                'Authorization: key='.$api_key,
                'Content-Type: application/json;charset=UTF-8'
            );
 
            $data = array
            (
              'data' =>
              array (
                'title' => $titulo,
                'body' => $corpo,
                'subtitle' => $legenda,
              ),
              'to' => '/topics/'.$topico,
              'priority' => 'high',
              //'restricted_package_name' => 'com.onlyoneapp.test', //IF YOU WANT SEND TO ONLY ONE APP
            );
 

            $content = json_encode($data);
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
            curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false );
            $result = curl_exec($curl);
            curl_close($curl);
            $arr = array();
            $arr = json_decode($result,true);
 
            if ($arr === FALSE) {
                echo "Json invalido!"."<br>";
            } else if (empty($arr)) {
                echo "Json invalido!"."<br>";
            }else{
                if (array_key_exists ('message_id', $arr)){
                    echo "Messaggio Inviato! <br>Mensagem id: ".$arr['message_id']."<br>";
                }else{
                    echo "Ocorreu um erro ao enviar a notificação!"."<br>";
                }
            }
?>
Includo il programma che riceve dal server..
Ho un problema.. per farlo funzionare nel manifest ho dovuto impostare :
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
Ora con la versione 26 non mi permette di scrivere in memoria
e mi va in errore se eseguo codice del tipo
B4X:
If File.Exists(File.DirDefaultExternal,"sessione.bin") Then
        sessione_map.Initialize
        sessione_map = File.ReadMap(File.DirDefaultExternal,"sessione.bin")
se uso
B4X:
android:targetSdkVersion="14"
scrive ma non funziona il servizio Firebase come posso fare?
Grazie mille
 

Attachments

  • notifica1.zip
    10.7 KB · Views: 224
Last edited:

ken87

Active Member
Licensed User
Longtime User
Ciao ho messo una versione più piccola di 26 ora non riccordo...tipo 22.. come faccio a trasmettere più di una notifiche tipo 6 tutte insieme.. imaggino in array .. qundi il file php avrà un vettore giusto? Potete fare un esempio? Grazie e buona nottea tutti
 

ken87

Active Member
Licensed User
Longtime User
Ciao altra cosa che non ho capito come fermare il servizio quando faccio i log out del app ho scritto :
B4X:
Sub disconetti_Click 
    StopService("FirebaseMessaging")
End Sub
Ma non fa nulla continua a funzionare.
Sapete il motivo?
 

ken87

Active Member
Licensed User
Longtime User
ciao se abilito la ricezione con subcribe tipo prima utenza Marco poi devo cambiare utenza tipo Elena .. come faccio a interrompere la ricezione delle notifiche di marco per passare ad Elena
 
Top