从php发送时未将FCM消息传递到设备
我正在尝试在我的应用程序中实现FCM。现在,当我从firebase应用程序控制台发送消息时,我能够接收消息。但是,当我试图从服务器上的php代码发送消息时,消息无法发送到手机。但是,每当对FCM的调用完成并且没有任何错误指示时,我都会收到带有数字的消息_id。任何帮助都将不胜感激 PHP代码:从php发送时未将FCM消息传递到设备,php,Php,我正在尝试在我的应用程序中实现FCM。现在,当我从firebase应用程序控制台发送消息时,我能够接收消息。但是,当我试图从服务器上的php代码发送消息时,消息无法发送到手机。但是,每当对FCM的调用完成并且没有任何错误指示时,我都会收到带有数字的消息_id。任何帮助都将不胜感激 PHP代码: public function send_fcm() { $API_ACCESS_KEY = '*****************************'; $msg = array (
public function send_fcm() {
$API_ACCESS_KEY = '*****************************';
$msg = array ('message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...',
'vibrate' => 1,
'sound' => 1
);
$fields = array('to' => '/topics/mytopic',
'priority' => 'high',
'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));
$pushResult = curl_exec($ch);
curl_close($ch);
}
您应该使用
字段
数组提供通知
,我使用PHP
public static function toDevice($token , $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'to' => $token,
'notification' => [
"body" => $message ,
...
] ,
"data" => [
...
]
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "XXXXXXXXXXXXXXXXXXX",
'Content-Type: application/json'
);
$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_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result;
}
你的修正码是…这是经过测试的代码。
或点击以下链接:
推送通知不起作用,因为FCM令牌未在Firebase的门户中注册。只需从设备和到参数的位置接收注册令牌。brilliant@Ali。。。工作起来很有魅力!非常感谢你们,也感谢所有帮助我的人。我只是很惊讶你们的反应有多快!
public function send_fcm($token) {
$url = "https://fcm.googleapis.com/fcm/send";
$msg = array('message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...',
'vibrate' => 1,
'sound' => 1
);
$fields = array('to' => $token,
'priority' => 'high',
'data' => array('message' => $msg)
);
$headers = array(
'Authorization:key=*****************************',
'Content-Type:application/json'
);
$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, json_encode($fields));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
if ($result === FALSE) {
die('CURL FAILED ' . curl_error($ch));
}
$info = curl_getinfo($ch);
curl_close($ch);
return array('result' => $result, 'status' => $info['http_code']);
}