2016-09-15 71 views
4

從Firebase控制檯發送通知時通知正常工作。如何在PHP中使用fcm(firebase控制檯)將推送通知發送給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"}]} 

請建議我是什麼我做錯了嗎?我也使用相同的代碼爲Android與它的服務器密鑰和設備令牌,它工作正常...

+4

你需要設置'優先'=>'高' – Shubhank

+0

你上傳了APNS證書嗎? – Gandharv

+0

是的,我已經將開發APNS證書上傳到firebase –

回答

0

似乎返回成功。也許檢查您的應用程序註冊碼以查看手機中的令牌是否已更改。有時候會生成一個新的令牌。

+0

令牌是從手機生成的。我使用相同的令牌發送推送通知,但沒有收到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帕瑪爲我工作 – PriyankaChauhan

+0

如何將推送通知發送到多個令牌ID? –

+0

@TusharSaindane將密鑰名稱更改爲'registration_ids'並傳遞數組。 –

相關問題