Open Find-Detail opened 1 month ago
anyone can please test
function getAccessToken() {
$keyFilePath = __DIR__ . '/service-account.json'; // Path to your service account JSON file
$keyFileContents = file_get_contents($keyFilePath);
$key = json_decode($keyFileContents, true);
$client = new Google_Client();
$client->setAuthConfig($key);
$client->setScopes(SCOPES);
// Fetch access token
$accessToken = $client->fetchAccessTokenWithAssertion();
return $accessToken['access_token'];
}
// Update your sendAndoid function to use the access token
public static function sendAndoid(erLhcoreClassModelUserSession $session, $chat, $params = array())
{
$options = erLhcoreClassModelChatConfig::fetch('mobile_options')->data;
if (!isset($options['fcm_project_id']) || !isset($options['fcm_server_key'])) {
throw new Exception('FCM Project ID or Server Key is not set');
}
$url = 'https://fcm.googleapis.com/v1/projects/' . $options['fcm_project_id'] . '/messages:send';
$message = isset($params['msg']) ? preg_replace('#\[[^\]]+\]#', '',strip_tags($params['msg'])) : preg_replace('#\[[^\]]+\]#', '', erLhcoreClassChat::getGetLastChatMessagePending($chat->id));
$data = [
'message' => [
'token' => $session->device_token,
'notification' => [
'title' => $params['title'],
'body' => $message
],
'data' => [
'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
'server_id' => $session->token,
'm' => $params['title'],
'chat_type' => $params['chat_type'],
'msg' => $message,
'chat' => json_encode($chat->getState())
]
]
];
$headers = [
'Authorization: Bearer ' . getAccessToken(), // Get access token
'Content-Type: application/json'
];
$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_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
curl_close($ch);
$responseData = json_decode($result, true);
if (isset($responseData['error'])) {
throw new Exception('FCM Error: ' . $responseData['error']['message']);
}
return $responseData;
}
}
?>
mobile app is working after last update but visitor are not showing in mobile app
second Cloud Messaging API (Legacy) deprecated on 6/20/2023 so migrate to the latest Firebase Cloud Messaging API (HTTP v1) by 6/20/2024 because of this if you create new app in firebase FCM key not found kindly add Firebase Cloud Messaging API (HTTP v1)