본문 바로가기

php

FCM 주제단위로 전송하기

<?php
define("GOOGLE_SERVER_KEY", "FCM에서 발급받은키";

function send_fcm($message, $id) {
$url = 'https://fcm.googleapis.com/fcm/send';

$headers = array (
'Authorization: key='.GOOGLE_SERVER_KEY,
'Content-Type: application/json'
);
$fields = array (
'data' => array ("message" => $message),
'notification' => array ("body" => $message)
);
if(is_array($id)) {
$fields['registration_ids'] = $id;
} else {
$fields['to'] = $id;
}
$fields['priority'] = "high";
$fields = json_encode ($fields);
$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_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close ( $ch );

return $result;
}
$message_status= send_fcm("test", "/topics/구독한주제");

echo $message_status;
?>


php에서 주제단위로 전송할때 

"/topics/안드로이드에서 구독시킨 명을 넣어주면된다"


ps)안드로이드에서 주제 구독시키는법 FirebaseMessaging.getInstance().subscribeToTopic("구독시킬명");

'php' 카테고리의 다른 글

mongodb php 작성법  (0) 2019.03.22
PhP Storm 연동하기  (2) 2018.07.03
Address already in use (Bind failed) 에러 해결하기  (0) 2018.04.03