Skip to content
Advertisement

Set FCM channel ID in PHP for notifications

I’m trying to send FCM notification to Android device using PHP. My code is working for devices before Android O.

In Android O, we need to set channel ID in request as well to receive notifications, which I cannot figure out how to do.

I have done the necessary setup in the app and using Firebase Console I can receive a notification, but it fails when I try sending it through the PHP script.

I also looked into the link below but it doesn’t work for me.

Android FCM define channel ID and send notification to this channel through PHP

My PHP code:

    $notification = new Notification();
    $notification->setTitle("startSession");
    $notification->setBody($_POST['child_id']);
    $notification->setNotificationChannel('my_channel_id');
    $requestData = $notification->getNotification();

    $firebase_token = $_POST['token'];
    $firebase_api = 'my_value';
    $fields = array(
                     'to' => $firebase_token,
                     'data' => $requestData,
                   );
    $url = 'https://fcm.googleapis.com/fcm/send';

    $headers = array(
        'Authorization: key=' . $firebase_api,
        'Content-Type: application/json'
    );
    $ch = curl_init();
    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    // Execute post
    $result = curl_exec($ch);

    if($result === FALSE){

        die('Curl failed: ' . curl_error($ch));
        $return_obj = array('responseCode'=>'0' , 'responseMsg'=> 'Error inserting, please try 
         again.', 'errorCode'=>'1');
    }else{
        $return_obj = array('responseCode'=>'1' , 'responseMsg'=> 'Successfully inserted.', 
        'errorCode'=>'0');
    }

    // Close connection
    echo json_encode($return_obj);
    curl_close($ch);

My notification class code is:

<?php
class Notification{
    private $title;
    private $body;
    private $answer_value;
    private $android_channel_id;


    function __construct(){

    }

    public function setTitle($title){
        $this->title = $title;
    }

    public function setBody($body){
        $this->body = $body;
    }

    public function setAnswer($answer){
        $this->answer_value = $answer;
    }

    public function setNotificationChannel($channel){
        $this->android_channel_id = $channel;
    }


    public function getNotificatin(){
        $notification = array();
        $notification['title'] = $this->title;
        $notification['body'] = $this->body;
        $notification['answer'] = $this->answer_value;
        $notification['android_channel_id'] = $this->android_channel_id;

        return $notification;
    }
}
?>

I’m sure the syntax is correct as the notification still works on devices with Android < 26.

Any help will be really appreciated. Thanks!

Advertisement

Answer

You can set your channel Id in server side PHP

PHP server side

<?php
  function Notify($title,$body,$target,$chid)
   {

    define( 'API_ACCESS_KEY', 'enter here your API Key' );

  $fcmMsg = array(
    'title' => $title,
    'body' => $body,
    'channelId' => $chid,

  );
  $fcmFields = array(
    'to' => $target, //tokens sending for notification
    'notification' => $fcmMsg,

  );

  $headers = array(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
  );

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fcmFields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result . "nn";

}

?>

I define function Notify with 4 parameters title,body,target,chid to use it just call the function and define its parameters,

You must add your channelId in your client side (Android part), with out channelId you can’t get notifications in Android 8.0 and later

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement