duandai0373 2019-02-04 12:58
浏览 488

Firebase推送通知无效获取令牌号但仍无效

I am using firebase for sending push notification. It was working fine with old package name recently I changed package name of my app later notification is not working, again I registered new package name with firebase and added new google-services.json file in app folder and I get token number also but notification is not sending to mobile.

 <?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;
?>

And json result :

{"multicast_id":6575432603807598829,"success":1,"failure":0,"canonical_ids":0," results":[{"message_id":"0:1549340761166996%1ca91b70f9fd7ecd"}]}

Notification is not sending & i tried with in firebase cloud messaging --> notification section also there also not working.

  • 写回答

1条回答

  • duanfeigui6655 2019-02-20 12:45
    关注

    Try this

    $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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题