This is my fcm_test.php code
foreach ($allTokens as $token) { echo $token . "<br>"; // // 發(fā)送 Firebase 通知到 FCM 令牌的代碼開始 // // 通知的數(shù)據(jù)有效負(fù)載 $notification = [ 'title' => '通知的標(biāo)題', 'body' => '這是通知的正文。', 'android' => [ 'imageUrl' => 'https://blog.pushwoosh.com/blog/content/images/2021/09/Android-12-Updates-for-Push-Notification-Senders---Pushwoosh.png' ], ]; // 創(chuàng)建 HTTP 請求的頭部 $headers = [ 'Authorization: key=' . $serverKey, 'Content-Type: application/json' ]; // 創(chuàng)建 HTTP 請求的有效負(fù)載 $payload = [ 'to' => $token, 'notification' => $notification ]; // 將有效負(fù)載轉(zhuǎn)換為 JSON $jsonPayload = json_encode($payload); try { // 初始化 cURL 會話 $ch = curl_init('https://fcm.googleapis.com/fcm/send'); // 設(shè)置 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); // 執(zhí)行 cURL 會話 $response = curl_exec($ch); // 檢查 cURL 錯誤 if ($response === false) { throw new Exception('cURL 錯誤: ' . curl_error($ch)); } // 打印響應(yīng)(用于調(diào)試目的) echo '響應(yīng): ' . $response . PHP_EOL; // 關(guān)閉 cURL 會話 curl_close($ch); // 處理響應(yīng) if ($response === false) { throw new Exception('發(fā)送通知失敗。'); } else { echo '通知發(fā)送成功。' . "<br><br>"; } } catch (Exception $e) { echo '錯誤: ' . $e->getMessage(); } // 發(fā)送 FCM 令牌的代碼結(jié)束 }
I am able to get the title and body in Android notifications, but not the image. I tried various JSON syntaxes but none worked, I can only get the text.
When I use Firebase GUI, I can get the image.
When accessing images on Flutter UI, I use the following code:
print("${widget.message?.notification?.android?.imageUrl}");
If I'm wrong, please feel free to answer. This will be very helpful.
Here is a checklist you can try.
Make sure the form you submit images to has the enctype="multipart/form-data" attribute.
If you are using the API to send images, it should be
base64_encode($image_path);