這是我的 fcm_test.php 程式碼
foreach ($allTokens as $token) { echo $token . "<br>"; // // 發(fā)送 Firebase 通知到 FCM 令牌的代碼開始 // // 通知的數(shù)據(jù)有效負載 $notification = [ 'title' => '通知的標題', '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 請求的有效負載 $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); // 執(zhí)行 cURL 會話 $response = curl_exec($ch); // 檢查 cURL 錯誤 if ($response === false) { throw new Exception('cURL 錯誤: ' . curl_error($ch)); } // 打印響應(用于調(diào)試目的) echo '響應: ' . $response . PHP_EOL; // 關閉 cURL 會話 curl_close($ch); // 處理響應 if ($response === false) { throw new Exception('發(fā)送通知失敗。'); } else { echo '通知發(fā)送成功。' . "<br><br>"; } } catch (Exception $e) { echo '錯誤: ' . $e->getMessage(); } // 發(fā)送 FCM 令牌的代碼結束 }
我在 Android 通知中能夠取得標題和正文,但無法取得圖片。 我嘗試了多種 JSON 語法,但都無效,只能取得文字。
當我使用 Firebase GUI 時,我可以取得圖片。
在 Flutter UI 上存取圖片時,我使用以下程式碼:
print("${widget.message?.notification?.android?.imageUrl}");
如果我錯了,請隨意回答。這將非常有幫助。
這是一個你可以嘗試的檢查清單。
確保你提交圖片的表單具有 enctype="multipart/form-data" 屬性。
如果你正在使用 API 發(fā)送圖片,它應該是
base64_encode($image_path);