【问题标题】:Sending Firebase notification with PHP使用 PHP 发送 Firebase 通知
【发布时间】:2018-03-09 07:50:38
【问题描述】:

大家!我在使用 PHP 发送 FIrebase 通知时遇到问题。当我从 Firebase 控制台发送它时,我会收到通知,但是当我从 PHP 发送它时,我没有收到任何通知。

你知道是什么问题吗?

这是我的 PHP 代码:

<?php
$message = 'ojlaasdasdasd';
$title = 'ojla';
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';    $server_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5LnDZO2BpC2aoPMshfKwRbJAJfZL8C33qRxxxxxxxxxxxxL6';
$key = 'eqnlxIQ1SWA:APA91bGf1COAZamVzT4onl66_lEdE1nWfY7rIADcnt9gtNYnw7iWTwa7AYPYTHESFholkZ89ydCQS3QeL-lCIuiWTXiqoDREO0xhNdEYboPvqg8QsBYkrQVRlrCLewC4N-hHUja1NG4f';
$headers = array(
                'Authorization:key=' .$server_key,
                'Content-Type: application/json');

$fields = array
            (
                'to'        => $key,
                'notification'  => array('title' => $title,'body' => $message)
            );


$payload = json_encode($fields);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, '$path_to_fcm' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt( $ch,CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($ch);
echo $result;
curl_close($ch);


?>

我没有收到来自服务器的任何回声

【问题讨论】:

    标签: php firebase firebase-cloud-messaging


    【解决方案1】:

    通过php向android、ios发送firebase推送通知

        function sendPushNotification($to = '', $data = array()){
        
            $api_key = '';
            $fields = array('to' => $to, 'notification' => $data);
        
            $headers = array(
                'Content-Type:application/json',
                'Authorization:key='.$api_key
            );
            $url = 'https://fcm.googleapis.com/fcm/send';
        
            $ch = curl_init();
            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_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
            $result = curl_exec($ch);
            if ($result === FALSE) {
                die('FCM Send Error: ' . curl_error($ch));
            }
            curl_close($ch);
            return $result;
        }
        
        
        
        
        $to = ''; //Device token 
        // You can get it from FirebaseInstanceId.getInstance().getToken();
        $message = array(
        'title' => 'Hai',
        'body' => 'New Message');
        
        echo sendPushNotification($to, $message);
    

    【讨论】:

      【解决方案2】:
                      $tokenID = $valueTwo["TokenID"];
                      $cmMessage = array();
                      $cmMessage["type"] = 3;
                      $cmMessage["receiverMail"] = $receiverMail;
                      $cmMessage["ownerMail"] = $ownerMail;
                      $cmMessage["jsonData"] = $jsonData;
      
      
      
      
                      $url = "https://fcm.googleapis.com/fcm/send";
      
                      $title = "";
                      $body = "";
                      //$notification = array('title' => $title, 'body' => null, 'sound' => 'default', 'badge' => '1');
                      $arrayToSend = array('to' => $tokenID, 'priority' => 'high', 'data' => $cmMessage);
                      $json = json_encode($arrayToSend);
                      $headers = array();
                      $headers[] = 'Content-Type: application/json';
                      $headers[] = 'Authorization: key=' . $serverKey;
                      $ch = curl_init();
                      curl_setopt($ch, CURLOPT_URL, $url);
                      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
                      curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
                      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                      //Send the request
                      $response = curl_exec($ch);
                      //Close request
                      if ($response === FALSE) {
                          die('FCM Send Error: ' . curl_error($ch));
                      }
                      curl_close($ch);
      

      获取您的服务器密钥和令牌ID以发送写入它们...不要使用通知标签...就像我在代码中所做的那样...因为如果您编写通知标签,后台应用程序不会收到消息Cuz通知标签在前台执行,在后台显示自动通知..

      【讨论】:

        【解决方案3】:

        我使用以下函数向 Firebase 发送 HTTP 请求:

        function request($url, $body, $method = "POST", $header = "Content-type: application/x-www-form-urlencoded\r\n")
        {
            switch ($method) {
                case 'POST':
                case 'GET':
                case 'PUT':
                case 'DELETE':
                    break;
                default:
                    $method = 'POST';
                    break;
            }
            $options = array(
                'http' => array(
                    'header' => "$header",
                    'method' => "$method",
                    'content' => $body
                )
            );
            $context = stream_context_create($options);
            $data = file_get_contents($url, false, $context);
            $data = json_decode($data, true);
            return $data;
        }
        

        要执行该功能,我使用以下代码:

        $fcm_key = "";
        $body = array("data" => $data, "to" => "$fcm_token");
        
        $body = json_encode($body);
        $json = request("https://fcm.googleapis.com/fcm/send", $body, "POST", "Authorization: key=$fcm_key\r\nContent-Type:application/json");
        

        对我来说,这段代码没有任何问题。确保使用正确的 FCM 设备令牌 ($fcm_token) 并设置正确的 FCM 密钥 ($fcm_key)。

        【讨论】:

          【解决方案4】:

          试试这个代码。它对我来说工作正常。只需更改密钥和令牌

          <?php
          define('API_ACCESS_KEY','Api key from Fcm add here');
           $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
           $token='235zgagasd634sdgds46436';
          
               $notification = [
                      'title' =>'title',
                      'body' => 'body of message.',
                      'icon' =>'myIcon', 
                      'sound' => 'mySound'
                  ];
                  $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];
          
                  $fcmNotification = [
                      //'registration_ids' => $tokenList, //multple token array
                      'to'        => $token, //single token
                      'notification' => $notification,
                      'data' => $extraNotificationData
                  ];
          
                  $headers = [
                      'Authorization: key=' . API_ACCESS_KEY,
                      'Content-Type: application/json'
                  ];
          
          
                  $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL,$fcmUrl);
                  curl_setopt($ch, CURLOPT_POST, true);
                  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
                  $result = curl_exec($ch);
                  curl_close($ch);
          
          
                  echo $result;
          

          【讨论】:

          • 谢谢! 'icon' 你在哪里存储了 myIcon?是“图标”字段,当您向下滑动通知栏时显示的图标,因为当我向下滑动时,通知旁边没有任何图标
          • 不需要。应用程序开发人员将处理它。如果它有帮助,请投票并将答案标记为已接受
          • android开发者可以根据需求自定义通知
          • @iCoders 我实施了你的修复,它似乎工作!但是,每次我发送一个推送通知时,徽章计数器都会出现在 2 处。它也不会增加。有没有办法解决这个问题?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-07-21
          • 1970-01-01
          • 1970-01-01
          • 2018-02-28
          • 2017-09-15
          • 2016-09-26
          • 2022-07-29
          相关资源
          最近更新 更多