2016-10-21 36 views
2

我使用https://github.com/arnesson/cordova-plugin-firebase/来接收基于离子的应用上的Google Firebase消息。谷歌Firebase iOS推送在控制台中工作,但不在API中

设置证书后,安装插件并设置Firebase帐户我可以接收通过Firebase控制台发送的通知(在Android和iOS设备上)。

但是,当我通过Firebase API发送(https://firebase.google.com/docs/cloud-messaging/http-server-ref)时,只有android设备收到通知。我使用下面的代码:

$data = Array 
(
    [to] => <token> 
    [notification] => Array 
     (
      [title] => My Title 
      [text] => Notification test 
      [sound] => default 
      [vibrate] => 1 
      [badge] => 0 
     ) 

) 

$jsonData = json_encode($data); 
$ch  = curl_init("https://fcm.googleapis.com/fcm/send"); 
$header = array(
    'Content-Type: application/json', 
    "Authorization: key=".$gcmApiKey 
); 

curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, true); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 

curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); 

$result = curl_exec($ch); 
curl_close($ch); 

echo $result; 

返回任何错误:

{"multicast_id":904572753471539870406,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1477063422322568734%3d5c78243d5c7824"}]} 

什么可能是错误的?

+1

检查您向其发送的令牌应该是正确的设备ID – siddhesh

+0

该令牌是正确的。当我使用相同的令牌通过Firebase Console发送消息时,通知会到达。 –

回答

4

对于iOS,尝试在你的有效载荷在参数相加priority设置为highcontent_available设置为true

查看参数详情here

+1

增加priority ='high'解决了这个问题。谢谢! –

+0

我有同样的问题。我已经设置了'priority ='high',仍然无法使其工作。你能否告诉我是否还有别的东西? –

0
try this code 

function sendGCM($message, $id) { 


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

    $fields = array (
      'registration_ids' => array (
        $id 
      ), 
      'data' => array (
        "message" => $message 
      ) 
    ); 
    $fields = json_encode ($fields); 

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

    $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_POSTFIELDS, $fields); 

    $result = curl_exec ($ch); 
    echo $result; 
    curl_close ($ch); 
} 

?> 

也尝试在卷曲终端

curl -X POST --header "Authorization: key=<API_ACCESS_KEY>" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"<YOUR_DEVICE_ID_TOKEN>\",\"notification\":{\"body\":\"Yellow\"},\"priority":10}" 
+0

尝试'registration_ids'而不是'to'。也尝试使用'数据',而不是'通知'。尝试了很多组合。没有成功。 –

+0

试着用你的代码替换我的代码在那里可以看到一个小错误 – siddhesh

+0

我试过你的代码,没有成功。使用你的代码通知也不会到达android。 –

0

有点晚,但与工作示例,

我使用下面的代码为iOS和Android推,你缺少的prioritycontent_available领域

例如:

$url = 'https://fcm.googleapis.com/fcm/send'; 
$fields = array(
      'to' => $token, 
      'notification' => array('body' => $message , "sound" => "default"), 
      'data' => $message, 
      "sound"=> "default", 
      'priority' => "high" , 
      'content_available' => false 
     ); 
$headers = array(
      'Authorization:key = your-key', 
      'Content-Type: application/json' 
     ); 
$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); 
相关问题