I'm trying to send a message to a specific person using Google Firebase Cloud with PHP. The token of the person who receives the message is stored in the DB. Here is my code:
define('SERVER_API_KEY','MYSERVERAPIKEY');
define('FRIEND_TOKEN','FRIEND_TOKEN')
$message = array(
'title'=>'Bet invitation',
'body' =>'Notification from ...',
);
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => FRIEND_TOKEN,
'data' => $message
);
$headers = array(
'Content-Type: application/json',
'Authorization: key='.SERVER_API_KEY
);
$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_VERIFYHOST, 0);
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));
}
echo "Result: $result";
echo "Curl: ". curl_error($ch);
curl_close($ch);
The problem is that the specified user doesn't get a message. And when I test it through the Firebase console, it works... However, the error message is strangely not shown to me either:(
Try the below code, hope it will help.
$message=[
'title'=>'Bet invitation',
'body' =>'Notification from ...',
];
$send = $this->GoogleCloudMessaging($friend_token,$message);
echo $send;
I have now found the problem myself. I added "gcm_sender_id": "YOUR_FIREBASE_PROJECT_SENDER_ID"
to the manifest.json file and rewritten the script, here is the solution:
define('SERVER_API_KEY', 'MYSERVERAPIKEY');
$data = array(
"to" => "$RECIVER_TOKEN",
"notification" => array(
"title" => "Test-Title",
"body" => "Test-Message",
"icon" => "https://url/images/iamge.png",
"click_action" => "https://url")
);
$headers = array(
'Content-Type: application/json',
'Authorization: key=' . SERVER_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_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
Greetings to: ashokan, David Angulo and https://santhoshveer.com/web-push-notification/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.