【问题标题】:FCM some times got connection port 443 failed errorFCM 有时会出现连接端口 443 失败错误
【发布时间】:2017-08-01 13:58:23
【问题描述】:
【问题讨论】:
标签:
php
android
firebase
firebase-cloud-messaging
【解决方案1】:
这可能是这些原因之一。
- 服务器无法访问互联网
- 如果您在 localhost(您自己的 PC)上尝试它,它不会工作。请在您的服务器上进行测试。
-
您没有正确发送请求(以下是您的参考 PHP 代码)。
function sendFCMPushNotification(){
$server_key = //"enter your server key here";
$mobile_tokens = array("token 1", "token 2", "token n");
$n_data = array("title"=>"Test Notification", "attribute"=>"3999", "parameter 2"=>"value");
$json_data = [
"to" => implode($mobile_tokens),
"notification" => [
"title" => "FCM PUSH",
"body" => "Text to be shown under the title",
"click_action" => "MAIN_ACTIVITY" // activity to be opened in application, remove this if you dont require one.
],
"data" => $n_data
];
$data = json_encode($json_data);
$url = 'https://fcm.googleapis.com/fcm/send';
$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, $data);
$result = curl_exec($ch);
//print($result);
if ($result === FALSE) {
print_r('Oops! FCM Send Error: ' . curl_error($ch));
}else{
print_r('WoW! FCM SENT SUCCESSFULLY');
}
curl_close($ch);
}
}