1. 서버에서 push 보내기

<?php
include('../../inf.php');

	extract($_GET);

	function send_notification ($token, $title, $body)
	{
       $url = "https://fcm.googleapis.com/fcm/send";
       $serverKey = '서버키';
       $notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
       $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];
       $arrayToSend = array('to' => $token, 'notification' => $notification,'data' => $extraNotificationData,'priority'=>'high');
       $json = json_encode($arrayToSend);
       $headers = array();
       $headers[] = 'Content-Type: application/json';
       $headers[] = 'Authorization: key='. $serverKey;
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
       curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
       curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
       //Send the request
       $response = curl_exec($ch);
       //Close request
       if ($response === FALSE) {
       die('FCM Send Error: ' . curl_error($ch));
       }
       curl_close($ch);
	}


	$conn = mysqli_connect($dbip, $dbid, $dbps, $dbname, $dbport);
	if(!$conn) die('Could not connect: ' . mysqli_connect_error());	

//	$sql = "select DISTINCT token from $tb_mem where code=" . $code . " and id not in('".$id."') and device not in('2') and loginFlg='1' and alarm='1'";
	$sql = "select DISTINCT token from $tb_mem where device = '1' and alarm='1' and trim(token) != '' ";

//	echo $sql;
	$result = mysqli_query($conn,$sql);
	$tokens = array();
	if(mysqli_num_rows($result) > 0 ){
		//DB에서 가져온 토큰을 모두 $tokens에 담아 둔다.
		while ($row = mysqli_fetch_assoc($result)) {
//			$tokens[] = $row[token];
			send_notification($row[token], $title, $message);
		}
	}
	mysqli_close($conn);

?>

 

'APP > Android Studio' 카테고리의 다른 글

[android] web view hybrid app 개발 (1)  (0) 2021.03.21
[android] web view 구현  (0) 2020.05.20
[android] Notification PushAlarm 구현하기  (0) 2018.09.17

+ Recent posts