繁体   English   中英

使用 PHP 发送 Firebase 通知

[英]Sending Firebase notification with PHP

大家! 我在使用 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
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;

我使用以下函数向 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 )。

                $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 以发送写入...不要使用通知标签...就像我在代码中所做的那样...因为如果您编写通知标签,后台应用程序将不会收到 msg 因为通知标签在前台执行并在后台显示自动通知..

通过 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM