php - Firebase 推送通知不起作用获取令牌编号但仍然不起作用
问题描述
我正在使用 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,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1549340761166996%1ca91b70f9fd7ecd"}]}
通知没有发送&我在firebase云消息传递中尝试过——>通知部分也没有工作。
解决方案
尝试这个
$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);
推荐阅读
- nestjs - 关于 NestJS 拦截器执行顺序的说明
- javascript - Javascript 中的颜色选择器选项
- python-3.x - 散景悬停信息在情节的某些部分背后
- c++ - return 不停止函数,递归函数问题?(编程练习、动态编程、Levenshtein Back-trace)
- vb.net - 如何在 Windows 10 的音量控制上显示您的媒体播放器程序
- php - PHP:如果某些条件为真,则显示元素
- c# - 在 ASP.Net 中找不到资源错误
- javascript - 如何匹配 URL 但不是模式
- c# - 在 ASP.Net MVC 中使用 JSON 从 AngularJS 调用 MVC 控制器
- c# - 使用附加模式属性后,iText7 将签名转换为不可见签名