728x90
curl -sS https://getcomposer.org/installer | php
composer update
vi composer.json
- mikey179/vfsStream를 mikey179/vfsstream 으로 수정
php composer.phar require google/auth
php composer.phar require google/apiclient:^2.12.1
or
composer require google/auth
composer require google/apiclient:~2.12.1
<?php
$url = 'https://fcm.googleapis.com/v1/projects/fcmsample-b5d93/messages:send';
require_once ('./vendor/autoload.php');
putenv('GOOGLE_APPLICATION_CREDENTIALS=./fcm_auth.json');
$scope = 'https://www.googleapis.com/auth/firebase.messaging';
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes($scope);
$auth_key = $client->fetchAccessTokenWithAssertion();
echo $auth_key['access_token'];
$ch = curl_init();
//header 설정 후 삽입
$headers = array
(
'Authorization: Bearer ' . $auth_key['access_token'],
'Content-Type: application/json'
);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$title = "제목입니다.";
$message = "내용이 들어갑니다.";
$notification_opt = array (
'title' => $title,
'body' => $message
// 'image' => 'http://sowonbyul.com/original/totalAdmin/images/Icon-512.png'
);
$datas = array (
'test1' => '테스트 데이터1',
'test2' => '테스트 데이터2',
'test3' => '테스트 데이터3'
);
$android_opt = array (
'notification' => array(
'default_sound' => true
)
);
$message = array
(
'token' => 'cFDXq8L7SMaU-mKlh3Yeev:APA91bGjmdOrtwcB1OR1I1hP0o3LXPChp6AiXKPQ7N9X5PQfNV5Uc7FS0m4y7N7Zm6Sq18JilurTlVQjN33PgmkqCRzlDaGxd18kNJrAKEsFPZzl7C_oqIG8ycfcYi0jQ8aOhx9kFIS-',
'notification' => $notification_opt,
'android' => $android_opt
);
$last_msg = array (
"message" => $message
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($last_msg));
$result = curl_exec($ch);
if($result === FALSE){
// die('FCM Send Error: ' . curl_error($ch));
printf("cUrl error (#%d): %s<br>\n",
curl_errno($ch),
htmlspecialchars(curl_error($ch)));
}
echo $result;
?>
CI4
use Google\Client as Google_Client;
use Google_Service_Oauth2;
https://blog.naver.com/PostView.naver?blogId=whdals0&logNo=222683916484
728x90
'개발 > php' 카테고리의 다른 글
php Sendbird 채널 그룹 나가기 curl put 이용 (0) | 2024.05.28 |
---|---|
코드이그나이터 mogoDB 라이브러리 (0) | 2023.04.26 |
PHP - CURL (0) | 2023.03.28 |
[php] 지연 (0) | 2021.03.02 |
댓글