如何在Firebase云通知中使用核心PHP发送图像

col17t5w  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(121)

这是我的fcm_test.php代码

foreach ($allTokens as $token) {
    echo $token . "<br>";

    // 
    // code to send firebase notification to fcm tokens starts
    //


    // Data payload for the notification
    $notification = [
        'title' => 'Title of the notification new',
        'body' => 'This is the body of the notification.',
        'android' => [

            'imageUrl' => 'https://blog.pushwoosh.com/blog/content/images/2021/09/Android-12-Updates-for-Push-Notification-Senders---Pushwoosh.png'

        ],
    ];

    // Create the headers for the HTTP request
    $headers = [
        'Authorization: key=' . $serverKey,
        'Content-Type: application/json'
    ];

    // Create the payload for the HTTP request
    $payload = [
        'to' => $token,
        'notification' => $notification
    ];

    // Convert the payload to JSON
    $jsonPayload = json_encode($payload);

    try {
        // Initialize cURL session
        $ch = curl_init('https://fcm.googleapis.com/fcm/send');

        // Set cURL options
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload);

        // Execute the cURL session
        $response = curl_exec($ch);

        // Check for cURL errors
        if ($response === false) {
            throw new Exception('cURL error: ' . curl_error($ch));
        }

        // Print the response (for debugging purposes)
        echo 'Response: ' . $response . PHP_EOL;

        // Close the cURL session
        curl_close($ch);

        // Handle the response
        if ($response === false) {
            throw new Exception('Failed to send notification.');
        } else {
            echo 'Notification sent successfully.' . "<br><br>";
        }
    } catch (Exception $e) {
        echo 'Error: ' . $e->getMessage();
    }
    // ends code to send fcm tokens
}

字符串
我在Android通知上获得标题和正文,但无法获得图像。我尝试了多种JSON语法,但没有用。只收到短信
当我使用firbase gui我得到图像
要访问flutter ui上的图像,我正在使用此
第一个月
如果我说错了,你尽管回答。会很有帮助

l7mqbcuq

l7mqbcuq1#

这里有一个清单,你可以试试。
1.确保提交图像的表单具有enctype=“multipart/form-data”属性。
1.如果您使用API发送图像,则应在
base64_encode($image_path);

相关问题