Set FCM channel ID in PHP for notifications

醉酒当歌 提交于 2021-01-28 07:42:57

问题


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!


回答1:


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 . "\n\n";

}

?>

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



来源:https://stackoverflow.com/questions/58037761/set-fcm-channel-id-in-php-for-notifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!