2017-04-24 113 views
0

我使用Firebase發送來自Web的推送通知,但我無法發送推送通知,而我只能獲取名稱。如何使用Firebase發送來自Web的推送通知幫助我。我使用Firebase發送來自網絡的推送通知

這是我的php代碼。

<?php 
$DEFAULT_URL = 'https://pushnotificatioexample.firebaseio.com/.json'; 

$mytoken = $_POST['token']; 
$mymessage = $_POST['message']; 
echo $mytoken; 
echo '<br>'.'</br>'; 
echo $mymessage; 

$registrationIds = array($mytoken); 

$message = array 
     (
     'message' => 'My awesome message', 
     'title'  => 'My awesome title', 
     'subtitle' => 'My awesome subtitle', 
     'tickerText' => 'My awesome Ticker text', 
     'vibrate' => 1, 
     'sound'  => 1, 
     'largeIcon' => 'large_icon', 
     'smallIcon' => 'small_icon' 
     ); 

     $fields = array(
     'registration_ids' => $registrationIds, 
     'data' => $message 
     ); 

     $headers = array(
     'Authorization:key = AIzaSyC9M4KFwqdy3vjcIBx9TNO8M7IysADbY_s', 
     'Content-Type: application/json' 
     ); 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $DEFAULT_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); 
     echo $result; 
?> 
+0

你不是應該使用FCM端點('的https :// fcm.googleapis.com/fcm/send')而不是'https:// pushnotificatioexample.firebaseio.com/.json'? –

回答

0

它看起來像你從你的服務端腳本發送雲消息。根據Firebase(FCM)的官方文檔,您不必使用實時數據庫和全部網址。所以,你必須改變你的網址

$DEFAULT_URL = 'https://pushnotificatioexample.firebaseio.com/.json'; 

TO

$DEFAULT_URL = 'https://fcm.googleapis.com/fcm/send'; 

詳情請參見該Firebase Cloud Messaging HTTP Protocolhere

+0

感謝它的工作 – romil

+0

很高興知道這是行之有效的。請標記它的答案,並提高它。所以這對其他人會有所幫助。 –