2016-12-06 228 views
0

我試圖使用PHP代碼/網頁瀏覽器發送FCM。如何使用PHP發送FCM通知?

但問題是,當我發送郵件使用PHP的Web瀏覽器,它:

  • FCM通知只出現在虛擬設備。
  • FCM通知不會出現在真實的手機設備上。

而且我只能使用Firebase控制檯向真實的手機設備發送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

是。 Android設備 – Jamilah

+0

你能夠正確獲取模擬器的註冊令牌嗎?您是否嘗試使用簡單的cURL響應發送消息,並查看iut是否仍然沒有收到任何消息? –

回答

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

從手機當你試圖連接谷歌FCM它發送給你一個registration_id/deviceID無論你打電話給它。它看起來像下面 'fxRKtQfyP5Y:APA91bGkjkerOGAVCdZFy6f9G-gmXHJwRNAeKELIWMgddDKQW34MZw0LQPPluM1hKCzYsfcVVnuwbSJEjctAKs2JAaCLUEeSM6QEW7qVZ281O9TUOa8mJ4Hb8qNo_qHoc9eSNvsGjrKY' – Bokul

+0

確定。我會先試試它:)。 – Jamilah