웹앱(하이브리드앱)
FCM Push Sample - PHP
xavi2019
2020. 4. 22. 22:37
선행사항
- iOS 앱이 설치되면 "iOS"라는 FCM Push Topic을 구독하게 해야 함.(구현완료)
PHP 샘플 소스
- server_key : 앱개발자에게 받을 것
- url: 보내고 싶은 URL
<?php
// Push Server Key
$server_key = "YOUR SERVER KEY";
$url = "이동하고 싶은 URL";
//
// 푸시 발송 함수
//
function sendNotification($title = "", $body = "", $customData = [], $to = "", $serverKey = ""){
if($serverKey != ""){
ini_set("allow_url_fopen", "On");
$data =
[
"to" => $to,
"notification" => [
"body" => $body,
"title" => $title,
],
"data" => $customData
];
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n" .
"Authorization:key=".$serverKey
)
);
$context = stream_context_create( $options );
$result = file_get_contents( "https://fcm.googleapis.com/fcm/send", false, $context );
return json_decode( $result );
}
return false;
}
//
// 실제 토픽으로 푸시 발송하는 부분
//
sendNotification("알림", "1:1문의가 등록되었습니다.", ["url" => $url], "/topics/iOS", $server_key);
// 개별기기에 발송
sendNotification("알림", "1:1문의가 등록되었습니다.", ["url" => $url], "cnIS_vGN3E-GhIvILZ5mM_:APA91bF-UZstyy4iLZ5_38OdN5r1qhLRK-v3W1Re1Bk8DRrf2JQ-McZwIxT1yYDbU0qIIJGk-W-fNlyOtiYFD2sXM9lHtWzW_PQV2BWi5jqwqPn0Mz8Ozlwqm6QM3REZLiyQSKgXZ-C5", $server_key);
?>