2016-09-15 7 views
4

firebaseコンソールからの通知の送信中に通知が正常に機能しています。PHPでfcm(firebase console)を使ってiPhoneにプッシュ通知を送信する方法は?

firebase console

私は、iOSデバイス上のプッシュ通知を取得しています。ここで

私はFCMを使用してPHPでiPhoneにプッシュ通知を送信するために使用していたコード..

<?php $ch = curl_init("https://fcm.googleapis.com/fcm/send"); 

    //The device token. 
    $token = ""; 

    //Title of the Notification. 
    $title = "Carbon"; 

    //Body of the Notification. 
    $body = "Bear island knows no king but the king in the north, whose name is stark."; 

    //Creating the notification array. 
    $notification = array('title' =>$title , 'text' => $body); 

    //This array contains, the token and the notification. The 'to' attribute stores the token. 
    $arrayToSend = array('to' => $token, 'notification' => $notification); 
    //Generating JSON encoded string form the above array. 
    $json = json_encode($arrayToSend); 

    //Setup headers: 
    $headers = array(); 
    $headers[] = 'Content-Type: application/json'; 
    $headers[] = 'Authorization: key= abcdgfdk'; //server key here 

    //Setup curl, add headers and post parameters. 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);  

    //Send the request 
    $response = curl_exec($ch); 

    //Close request 
    curl_close($ch); 
    return $response; ?> 

であり、それは次の応答を返します。

{"multicast_id":7847791275395796141,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1473926169782959%51b989d251b989d2"}]} 

を私は何私を提案して下さい間違っている?私はアンドロイドのために同じコードをサーバーキーとデバイストークンで使用しています。それは正常に動作しています。

+4

あなたが必要'priority' => 'high'を設定するには – Shubhank

+0

あなたはAPNS証明書をアップロードしましたか? – Gandharv

+0

はい、firebase –

答えて

0

成功を返すと思われます。アプリの登録コードをチェックして、電話機のトークンが変更されているかどうかを確認してください。新しいトークンが生成されることがあります。

+0

に開発APNS証明書をアップロードしました。電話機からトークンが生成されました。私はプッシュ通知を送信するのに同じトークンを使用していますが、iPhoneで受信しませんでした –

9

おかげであなたの答えは私が追加する必要がある唯一のものは優先順位の高いです...働く.. shubank ...ここで更新されたコードがある...それはあまりにも誰かを助けるかもしれない:)

$ch = curl_init("https://fcm.googleapis.com/fcm/send"); 

    //The device token. 
    $token = ""; //token here 

    //Title of the Notification. 
    $title = "Carbon"; 

    //Body of the Notification. 
    $body = "Bear island knows no king but the king in the north, whose name is stark."; 

    //Creating the notification array. 
    $notification = array('title' =>$title , 'text' => $body); 

    //This array contains, the token and the notification. The 'to' attribute stores the token. 
    $arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high'); 

    //Generating JSON encoded string form the above array. 
    $json = json_encode($arrayToSend); 
    //Setup headers: 
    $headers = array(); 
    $headers[] = 'Content-Type: application/json'; 
    $headers[] = 'Authorization: key= $key'; // key here 

    //Setup curl, add headers and post parameters. 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);  

    //Send the request 
    $response = curl_exec($ch); 

    //Close request 
    curl_close($ch); 
    return $response; 
+0

Simerjit parmarが私のために働いてくれてありがとう – PriyankaChauhan

+0

プッシュ通知を複数のトークンIDに送信するにはどうすればいいですか? –

+0

@TusharSaindaneはキー名をtoから 'registration_ids'に変更し、配列を渡します。 –

関連する問題