Firebase推送通知不起作用获取令牌号,但仍然不起作用

时间:2019-02-04 12:58:02

标签: php android firebase google-cloud-messaging firebase-cloud-messaging

我正在使用Firebase发送推送通知。最近使用旧软件包名称可以正常使用,我更改了应用程序的软件包名称,后来通知不起作用,再次在firebase中注册了新软件包名称,并在应用程序文件夹中添加了新的google-services.json文件,我也获得了令牌编号,但通知没有发送到手机。

 <?php 
//Getting api key 
$api_key = 'api_key'; 

//Getting registration token we have to make it as array 
$token = 'token_number';
$reg_token = array($token);

//Getting the message 
$message = 'Test Message Success!!';

//Creating a message array 
$msg = array
(
    'message' => $message,
    'title' => 'Test Message',
    'subtitle' => 'Android Push Notification using FCM Demo',
    'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate' => 1,
    'sound' => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'
);

//Creating a new array fileds and adding the msg array and registration token array here 
$fields = array
(
    'registration_ids' => $reg_token,
    'data' => $msg
);

//Adding the api key in one more array header 
$headers = array
(
    'Authorization: key=' . $api_key,
    'Content-Type: application/json'
); 

//Using curl to perform http request 
$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 ) );

//Getting the result 
$result = curl_exec($ch );
curl_close( $ch );

//Decoding json from result 
// $res = json_decode($result);


//Getting value from success 
// $flag = $res->success;
?>

和json结果:

{“ multicast_id”:6575432603807598829,“成功”:1,“失败”:0,“ canonical_ids”:0,“  结果“:[{” message_id“:” 0:1549340761166996%1ca91b70f9fd7ecd“}]}

通知未发送,我在Firebase云消息传递中尝试过->通知部分也无法正常工作。

1 个答案:

答案 0 :(得分:0)

尝试一下

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

$fields = array (
        'registration_ids' => $ids,
         // fix new json format
        'notification' => array(
            'body' => 'Test message :)',
            'title' => 'Test',
        )

);

$fields = json_encode($fields);

$server_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// inherited key

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

$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, $fields);
$result = curl_exec($ch);

var_dump($result);

if ($result === FALSE) {
    die('Oops! FCM Send Error: ' . curl_error($ch));
}

curl_close($ch);