2016-12-06 4 views
0

PHP 코드/웹 브라우저를 사용하여 FCM을 보내려고했습니다.PHP를 사용하여 FCM 알림을 보내는 방법은 무엇입니까?

  • FCM 알림은 가상 장치에 나타납니다 : 나는 그것이 PHP 웹 브라우저를 사용하여 보낼 때

    는 그러나 문제이다.

  • 실제 전화 장치에는 FCM 알림이 표시되지 않습니다.

그리고 파이어베이스 콘솔을 사용하여 실제 전화 장치로만 FCM 알림을 보낼 수 있습니다.

누군가 도울 수 있습니까? 코드는 다음과 같습니다.

<?php 
require "init.php"; 
global $con; 
    if(isset($_POST['Submit'])){ 
     $message  = $_POST['message']; 
     $title  = $_POST['title']; 
     $path_to_fcm = 'https://fcm.googleapis.com/fcm/send'; 
     $server_key = "AAAA2gV_U_I:APA91bHA28EUGmA3BrDXFInGy-snx8wW6eZ_RUE7EtOyM99pbfrVZU_ME-FU0O9_dUxYpM30OYF8KWYlixod_PfwbgLNoovzdkdJ4F-30vY8X_tBz0CMrajCIAgbNVRfw203YdRGli";  
     $sql  = "SELECT fcm_token FROM fcm_table"; 
     $result = mysqli_query($con, $sql); 
     $row  = mysqli_fetch_row($result); 
      $key = $row[0]; 
     $headers = array('Authorization:key=' .$server_key, 'Content-Type:application/json'); 
     $fields = array('to' => $key, 'notification' => array('title' => $title, 'body'=> $message)); 
     $payload = json_encode($fields); 
     $curl_session = curl_init(); 
     curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm); 
     curl_setopt($curl_session, CURLOPT_POST, true); 
     curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); 
     curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload); 
     $result = curl_exec($curl_session); 
     curl_close($curl_session); 
     mysqli_close($con); 
    } 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>FCM Notification</title> 
</head> 
<body> 
    <form action='fcm_notification.php' method="POST"> 
     <table> 
      <tr> 
       <td>Title : </td> 
       <td><input type="text" name="title" required="required" /></td> 
      </tr> 
      <tr> 
       <td>Message : </td> 
       <td><input type="text" name="message" required="required" /></td> 
      </tr> 
      <tr> 
       <td><input type="submit" name="Submit" value="Send notification"></td> 

      </tr> 
     </table> 
    </form> 
</body> 
</html> 

감사합니다.

+0

기기별로 보면 Android가 맞습니까? –

+0

예. 안드로이드 장치 – Jamilah

+0

에뮬레이터에 대한 등록 토큰을 제대로 얻을 수 있습니까? 간단한 cURL 응답을 사용하여 메시지를 보내려고했지만 아직 메시지를받지 못했는지 확인하십시오. –

답변

1

Google FCM을 사용하여 모바일에 푸시 알림을 보낼 수있는 방법은 다음과 같습니다. 나를 위해 그 작품은 예상대로. 키 추가 'priority' => 'high'

function sendPushNotification($fields = array()) 
{ 
    $API_ACCESS_KEY = 'YOUR KEY'; 
    $headers = array 
    (
     'Authorization: key=' . $API_ACCESS_KEY, 
     'Content-Type: application/json' 
    ); 
    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send'); 
    curl_setopt($ch,CURLOPT_POST, true); 
    curl_setopt($ch,CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields)); 
    $result = curl_exec($ch); 
    curl_close($ch); 
    return $result; 
} 

$title = 'Whatever'; 
$message = 'Lorem ipsum'; 
$fields = array 
(
    'registration_ids' => ['deviceID'], 
    'data'   => '', 
    'priority' => 'high', 
    'notification' => array(
     'body' => $message, 
     'title' => $title, 
     'sound' => 'default', 
     'icon' => 'icon' 
    ) 
); 

sendPushNotification($fields); 
+0

deviceID? FCM의 발신자 ID와 동일합니까? – Jamilah

+0

모바일에서 Google FCM에 연결하려고하면 전화를 건 무엇이든 registration_id/deviceID를 보냅니다. APA91bGkjkerOGAVCdZFy6f9G-gmXHJwRNAeKELIWMgddDKQW34MZw0LQPPluM1hKCzYsfcVVnuwbSJEjctAKs2JAaCLUEeSM6QEW7qVZ281O9TUOa8mJ4Hb8qNo_qHoc9eSNvsGjrKY' – Bokul

+0

확인 : 그것은 'fxRKtQfyP5Y 아래처럼 보인다. 나는 그것을 먼저 시도 할 것이다 :). – Jamilah

관련 문제