Google Firebase iOS推送适用于控制台,但不适用于API

时间:2016-10-21 17:27:40

标签: php cordova cordova-plugins firebase-cloud-messaging

我正在使用https://github.com/arnesson/cordova-plugin-firebase/在基于离子的应用上接收Google Firebase消息。

设置证书后,安装插件并设置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"}]}

有什么不对?

3 个答案:

答案 0 :(得分:6)

对于iOS,请尝试在您的有效负载中添加设置为priorityhigh设置为content_available的参数true

参见参数详情here

答案 1 :(得分: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终端中试试

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}"

答案 2 :(得分:0)

有点晚了但是有了实例,

我使用下面的代码进行ios和android push,你错过了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);