2017-02-09 77 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框架在字段对象中更改此字段 –