我正在尝试使用FCM从PHP推动通知。这是我的代码。
$serverKey = 'MYKEY';
define('API_ACCESS_KEY', $serverKey);
$registrationIds = ($vals['registration_ids']);
$msg=$vals['data'];
// prep the bundle
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
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_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
但总是说:
{" MultiCast_Id":9213457463518601541,"成功":0,"失败":7," Canonical_ids":0,"结果":[rorral':[{" error":" notegrented"}"},{" error":" notregistered"},{" error":" not registered"},{" error":" invalidRegistration"},{" error":" invalidRegrigration"},{invalidRegration"},{"}]}
尝试该代码:
<?php
public function notification($registrationIds,$body,$title)
{
$body;
$title;
$registrationIds;
//#API access key from Google API's Console
if(!defined('AIdfSyCd8ha2wop84LKtpQvRmLpeTg2CEiY8Z')){
define( 'AIdfSyCd8ha2wop84LKtpQvRmLpeTg2CEiY8Z', 'AAAA61DTuxA:APA93BGY4CXhKXJ2tPjoh3dnhbkmn1bGmfFj6Q7JJaBrdqKI106NFc3LZQTHy5i8WO5JoQBalrCIW_Cs2pGUz5mp1OL9SVcx0oH-hRGGpaeqV6hF-UDqJAlWAjGwyQM-oSl98ye_zMDi' );
}
$registrationIds = $registrationIds;
//#prep the bundle
$msg = array
(
'body' => $body,
'title' => $title,
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$fields = array
(
'to' => $registrationIds,
'notification' => $msg
);
$headers = array
(
'Authorization: key=AAAA61DTuxA:APA93BGY4CXhKXJ2tPjoh3dnhbkmn1bGmfFj6Q7JJaBrdqKI106NFc3LZQTHy5i8WO5JoQBalrCIW_Cs2pGUz5mp1OL9SVcx0oH-hRGGpaeqV6hF-UDqJAlWAjGwyQM-oSl98ye_zMDi',
'Content-Type: application/json'
);
#Send Reponse To FireBase Server
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
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_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
#Echo Result Of FireBase Server
$result;
}
?>