Maison  >  Questions et réponses  >  le corps du texte

Tutoriel de notification Firebase Cloud pour l'envoi d'images à l'aide de Core PHP

Voici mon code fcm_test.php

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

    // 
    // 发送 Firebase 通知到 FCM 令牌的代码开始
    //



    // 通知的数据有效负载
    $notification = [
        'title' => '通知的标题',
        'body' => '这是通知的正文。',
        'android' => [

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

        ],
    ];

    // 创建 HTTP 请求的头部
    $headers = [
        'Authorization: key=' . $serverKey,
        'Content-Type: application/json'
    ];

    // 创建 HTTP 请求的有效负载
    $payload = [
        'to' => $token,
        'notification' => $notification
    ];

    // 将有效负载转换为 JSON
    $jsonPayload = json_encode($payload);

    try {
        // 初始化 cURL 会话
        $ch = curl_init('https://fcm.googleapis.com/fcm/send');

        // 设置 cURL 选项
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload);

        // 执行 cURL 会话
        $response = curl_exec($ch);

        // 检查 cURL 错误
        if ($response === false) {
            throw new Exception('cURL 错误: ' . curl_error($ch));
        }

        // 打印响应(用于调试目的)
        echo '响应: ' . $response . PHP_EOL;


        // 关闭 cURL 会话
        curl_close($ch);

        // 处理响应
        if ($response === false) {
            throw new Exception('发送通知失败。');
        } else {
            echo '通知发送成功。' . "<br><br>";
        }
    } catch (Exception $e) {
        echo '错误: ' . $e->getMessage();
    }
    // 发送 FCM 令牌的代码结束
}

Je peux obtenir le titre et le corps dans les notifications Android, mais pas l'image. J'ai essayé différentes syntaxes JSON mais aucune n'a fonctionné, je ne peux obtenir que le texte.

Lorsque j'utilise l'interface graphique de Firebase, je peux obtenir l'image.

Lorsque j'accède aux images sur Flutter UI, j'utilise le code suivant :

print("${widget.message?.notification?.android?.imageUrl}");

N'hésitez pas à répondre si je me trompe. Cela sera très utile.

P粉788571316P粉788571316372 Il y a quelques jours581

répondre à tous(1)je répondrai

  • P粉541565322

    P粉5415653222023-09-17 09:05:22

    Voici une liste de contrôle que vous pouvez essayer.

    1. Assurez-vous que le formulaire auquel vous soumettez l'image possède l'attribut enctype="multipart/form-data".

    2. Si vous utilisez l'API pour envoyer des images, cela devrait l'être
      base64_encode($image_path);

    répondre
    0
  • Annulerrépondre