【问题标题】:Set FCM channel ID in PHP for notifications在 PHP 中为通知设置 FCM 通道 ID
【发布时间】:2019-09-21 06:14:24
【问题描述】:

我正在尝试使用 PHP 向 Android 设备发送 FCM 通知。我的代码适用于 Android O 之前的设备。

在 Android O 中,我们还需要在请求中设置通道 ID 以接收通知,我不知道该怎么做。

我已经在应用程序中完成了必要的设置,并且使用 Firebase 控制台我可以收到通知,但是当我尝试通过 PHP 脚本发送它时它失败了。

我也查看了下面的链接,但它对我不起作用。

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

我的 PHP 代码:

    $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);

我的通知类代码是:

<?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;
    }
}
?>

我确信语法是正确的,因为通知仍然适用于 Android

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 哪部分不工作请说明?

标签: php android firebase firebase-cloud-messaging


【解决方案1】:

您可以在服务器端 PHP 中设置您的频道 ID

PHP 服务器端

<?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";

}

?>

我用 4 个参数 title,body,target,chid 定义函数 Notify 来使用它,只需调用函数并定义其参数,

您必须在客户端(Android 部分)中添加您的 channelId,并使用 在 Android 8.0 及更高版本中,您无法收到 channelId 的通知

【讨论】:

  • 谢谢!问题是,我使用的是密钥:“android_channel_id”。将其更改为“channelId”并且效果很好。没找到其他地方写的。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 2019-01-26
  • 2020-08-26
  • 1970-01-01
  • 2019-09-06
  • 2019-04-13
相关资源
最近更新 更多