Android Example Send Firebase notification via PHP (CURL)

Discussion in 'Tutorials & Examples' started by Douglas Farias, Oct 17, 2018.

  1. Douglas Farias

    Douglas Farias Expert Licensed User

    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($curlCURLOPT_HEADERfalse);
                
    curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
                
    curl_setopt($curlCURLOPT_HTTPHEADER$headers);
                
    curl_setopt($curlCURLOPT_POSTtrue);
                
    curl_setopt($curlCURLOPT_POSTFIELDS$content);
                
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEERfalse );
                
    $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>";
                    }
                }
    ?>
     

    Attached Files:

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice