Код: Выделить всё
function test_android_push_notification() {
$scope = 'https://www.googleapis.com/auth/firebase.messaging';
$appName = 'XXXXX_App';
$client = new \Google\Client();
$client->setAuthConfig('PATH_OF_THE_JSON_FILE.json');
$client->useApplicationDefaultCredentials();
$client->setScopes($scope);
$client->setApplicationName($appName);
$access_token = $client->fetchAccessTokenWithAssertion();
$bearerToken = $access_token['access_token']; // BEARER TOKEN RECEIVED
//HTTP V1 API end-point
$url = 'https://fcm.googleapis.com/v1/projects/PROJECT_NAME_HERE/messages:send';
$token = trim($this->request->getPost('token')); // DEVICE TOKEN
$data = array("message" => array("token" => $token, "data" => array("score" => "83", 'time' => "15:30"), "android" => array("direct_boot_ok" => true)));
//header with content_type api key
$headers = array(
'Content-Type:application/json',
'Authorization: Bearer ' . $bearerToken
);
//CURL request to route notification to FCM connection server (provided by Google)
$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($data));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Oops! FCM Send Error: ' . curl_error($ch));
} else {
echo '-----Notification Sent';
}
curl_close($ch);
return $result;
}
Код: Выделить всё
"error": {
"code": 403,
"message": "SenderId mismatch",
"status": "PERMISSION_DENIED",
"details": [
{
"@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "SENDER_ID_MISMATCH"
}
]
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... r-id-misma
Мобильная версия