Android Example Send Firebase notification via PHP (CURL)

Hi all.
I found this example in some old folders on my pc and decided to share here.
it is a simple example of how send a firebase notification via php

PHP:
<?php
        $url = "https://fcm.googleapis.com/fcm/send";
        $topico = "general";
        $api_key = "asdqw5d41q8wd4qw8d4qw8de4q8554d8qw4d84"; //FIREBASE KEY
        $titulo = "Notification Title";
        $corpo = "Notification Message...";
        $legenda = "SubTitle...";
     

            $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 "Mensagem enviada! <br>Mensagem id: ".$arr['message_id']."<br>";
                }else{
                    echo "Ocorreu um erro ao enviar a notificação!"."<br>";
                }
            }
?>
 

Attachments

  • firebasephp.zip
    882 bytes · Views: 654
Top