2017-02-09 42 views
0

我的Android應用程序可以從Firebase控制檯接收FCM,但不是來自我的應用程序服務器。Android應用程序無法從我的應用程序服務器接收到FCM通知

這是我的php腳本片段。

$target = 'device code'; 
$data = array('message' => 'Hello','title'=>'FCM Push Notifications'); 

function sendFCMMessage($data,$target){ 
    //FCM API end-point 
    $url = 'https://fcm.googleapis.com/fcm/send'; 
    $server_key = 'server key';  

    $fields = array(); 
    $fields['data'] = $data; 
    $fields['priority'] = 'high'; 
    if(is_array($target)){ 
    $fields['registration_ids'] = $target; 
    }else{ 
    $fields['to'] = $target; 
    } 
    //header with content_type api key 
    $headers = array(
    'Content-Type:application/json', 
     'Authorization:key='.$server_key 
    ); 
    //CURL request to route notification to FCM connection server (provided by Google)   
    $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('Oops! FCM Send Error: ' . curl_error($ch)); 
    } 
    curl_close($ch); 
    return $result; 
} 

$pn = sendFCMMessage($data,$target); 
echo $pn; 

但我沒有收到成功應答運行我的腳本像下面這樣一個後。

{ 
    "multicast_id":4696016312851064849, 
    "success":1, 
    "failure":0, 
    "canonical_ids":0, 
    "results":[ 
     { 
     "message_id":"0:1486632797062481%1749d09ff9fd7ecd" 
     } 
    ] 
} 
+0

添加PHP的標誌,因爲大多數人使用開發Android其他語言。我不知道它是如何工作的,但也許一個日誌應該可以幫助他人瞭解什麼是錯誤的並給你解決方案。 –

+0

嗨。從Firebase控制檯發送消息時,它將被視爲「通知」有效內容。在你的PHP腳本中,你只發送一個「數據」消息負載。查看兩個[這裏]的區別(https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages)。您可能只處理客戶端應用程序中的「通知」消息。你可以發佈你的Android端代碼嗎? –

+0

@AL。謝謝 !我忽略了那部分。我在這裏找到了我的答案。 –

回答

0
this is my working code change you php code similar to this but change parrameter    



       $apiKey = "fjdfhdjskajklsjdsalkdsajkldsakdjksadjksadsa-";  

       foreach($data as $deviceid) 
       { 
        $arydevice[]=$deviceid->gcmregid; 
       } 

      //print_r($arydevice); 

       $count = count($arydevice); 

       for ($i = 0; $i < $count; $i++) { 

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

        $fields=array('to'=>(string)$arydevice[$i] ,'notification'=>array('title'=>'title','body'=>"1",'click_action'=>'click action','icon'=>'ic_stat_final','sound'=>'default','color'=>'#00aff0'),'data'=>array('multicast_id'=>$mid,'success'='0','schoolid'=>$SchoolId,'timestamp'=>$timestamp,'title'=>$title)); 


        $headers = array(
         'Authorization: key=' . $apiKey, 
         'Content-Type: application/json' 
        );  
        //print_r($fields); 
        //print_r($headers); 
        //die; 



    //var_dump($payload); 


        // Open connection 
        $ch = curl_init();   
        // Set the url, number of POST vars, POST data 
        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_VERIFYPEER, false);  
        curl_setopt($ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

        // Execute post 
        $result = curl_exec($ch); 

        curl_close($ch); 
       }} 
+0

第一個設備ID應該是字符串和數據無論您希望您作爲數據鍵中的鍵值對發送的任何數據 –

+0

=>使用yii框架在字段對象中更改此字段 –

相關問題