php fcm 사용 예제

곽준영·2023년 3월 7일
0

위 이미지의 "서버 키"를 밑에 예제에 사용한다.

token 값은 밑에 코드를 통해 구해서 임시적으로 사용한다.(기기별로 토큰이 다름) - flutter에서

final fcmToken = await FirebaseMessaging.instance.getToken();
print("fcmToken: $fcmToken");

php fcm 예제

function send_notification($tokens, $message) {

			$url = 'https://fcm.googleapis.com/fcm/send';
		
			if ($message) {		
				$fields = array (		
					'notification' => array ('body' => $message['body'], 'title' => $message['title'])		
				);		
			}
		
			if(is_array($tokens)) {		
				$fields['registration_ids'] = $tokens;		
			} else {		
				$fields['to'] = $tokens;		
			}
		
			$fields['priority'] = "high";		
		
			$FCM_SERVER_KEY = "자기 서버 키 넣기"; 
		
			$headers = array(		
				'Authorization:key =' . $FCM_SERVER_KEY,		
				'Content-Type: application/json'		
			);

		
			$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_SSL_VERIFYHOST, 0);  		
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);		
			curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
		
			$result = curl_exec($ch);           
		
			if ($result === FALSE) {		
			   die('Curl failed: ' . curl_error($ch));		
			}
		
			curl_close($ch);
		
			return $result;
		
		}
		
		
		$tokens = "기기별 토큰 넣기";
		
		
		$message = array(		
			"title"   => "$title", 		
			"body" => "$content", 		
		);
		
		send_notification($tokens, $message);
profile
I want to become a versatile freelancer programmer💻

0개의 댓글