Merhaba,
Bildirim işlemlerinin tamamını PHP tarafında yaptığımı belirtmek isterim.
Sunucu tarafında gerekli kütüphane kurulumu için aşağıdaki komutu sunucudaki ana dizin içerisinde çalıştırınız. (html veya htdocs -- Windows | Linux )
composer require google/apiclient
PHP alanında IOS cihazdan alınan APN bilgisinin FCM'ye uygun hale getirmek için :
<?php
require 'vendor/autoload.php';
use Google\Client;
$serviceAccountPath = 'indirilenJsonAdı.json';
function getAccessToken($serviceAccountPath) {
$client = new Client();
$client->setAuthConfig($serviceAccountPath);
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$client->useApplicationDefaultCredentials();
$token = $client->fetchAccessTokenWithAssertion();
if (isset($token['error'])) {
throw new Exception( $token['error_description']);
}
return $token['access_token'];
}
$accessToken = getAccessToken($serviceAccountPath);
$iidurl = 'https://iid.googleapis.com/iid/v1:batchImport';
if (isset($_GET['apn'])) { $apn = $_GET['apn']; }
if (isset($_GET['sandbox'])) { $sandbox = $_GET['sandbox']; }
if ((isset($apn)) && (isset($sandbox)))
{
$sandbox_bool = ($sandbox == 'true');
$iidbody = json_encode([
"application" => "com.paketAdi.uygulamaAdi",
"sandbox" => $sandbox_bool,
"apns_tokens" => [
$apn
]
]);
$iidch = curl_init($iidurl);
curl_setopt($iidch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($iidch, CURLOPT_POST, true);
curl_setopt($iidch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $accessToken,
'Content-Type: application/json',
'access_token_auth: true'
]);
curl_setopt($iidch, CURLOPT_POSTFIELDS, $iidbody);
$iidresponse = curl_exec($iidch);
if (curl_errno($iidch)) {
echo curl_error($iidch);
} else {
echo $iidresponse ;
}
curl_close($iidch);
}
else {
$data = array(
"Result" => array(
array(
"ResultStatus" => "0",
"ResultMessage" => "TOKEN ERROR"
)
)
);
echo json_encode ($data);
}
?>
Kullanımı :
http://localhost/phpAdi.php?apn=01992fd551185ef0fc546546546435342fe0245605b98e&sandbox=true
"indirilenJsonAdı.json" bilgisini Firebase - Proje ayarları alanından "Generate new private key" kısmından indirip php dosyasının bulunduğu yere kaydediniz.
[
attachment=2776]
Ek olarak
"project ID" bilgisi de bildirim yollarken gerekli ve onu da yine aynı alandaki aşağıdaki sekmeden alabilirsiniz.
[
attachment=2777]
PHP tarafında hedeflenen TOKEN bilgisine bildirim göndermek için :
<?php
require 'vendor/autoload.php';
use Google\Client;
function getAccessToken($serviceAccountPath) {
$client = new Client();
$client->setAuthConfig($serviceAccountPath);
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$client->useApplicationDefaultCredentials();
$token = $client->fetchAccessTokenWithAssertion();
if (isset($token['error'])) {
throw new Exception($token['error_description']);
}
return $token['access_token'];
}
function sendMessage($accessToken, $projectId, $message) {
$url = 'https://fcm.googleapis.com/v1/projects/' . $projectId . '/messages:send';
$headers = [
'Authorization: Bearer ' . $accessToken,
'Content-Type: application/json',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['message' => $message]));
$response = curl_exec($ch);
if ($response === false) {
throw new Exception('Curl error: ' . curl_error($ch));
}
curl_close($ch);
return json_decode($response, true);
}
$serviceAccountPath = 'indirilenJsonAdi.json';
$projectId = 'projectID';
$message = [
'notification' => [
'title' => 'Örnek Başlık',
'body' => 'Örnek içerik',
],
'token' => 'TOKEN_BILGISI'
];
try {
$accessToken = getAccessToken($serviceAccountPath);
$response = sendMessage($accessToken, $projectId, $message);
print_r( $response);
} catch (Exception $e) {
echo $e->getMessage();
}
?>
Şimdilik bu kadar...
Takıldığınız yer olursa yardımcı olurum.