【发布时间】:2017-01-16 06:42:10
【问题描述】:
我正在使用 FCM 向带有 MySQL 和 PHP 的 Android 发送推送通知。这是我的代码:
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = YOUR_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));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$conn = mysqli_connect("localhost","root","","fcm");
$sql = " Select Token From users";
$result = mysqli_query($conn,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["Token"];
}
}
mysqli_close($conn);
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
但我尝试在 iOS(Swift) 设备上发送通知,但它不起作用。我得到了令牌 (FIRInstanceID.instanceID().token()) 并尝试从 send.php (以上代码) 发送通知但没有成功。
我从Firebase Console 发送个人/组/主题通知并且工作得很好。为什么它在file.php 中不起作用?
帮助?!
【问题讨论】:
-
什么是 MySQL,你为什么要撤消我的编辑?
标签: ios swift firebase firebase-cloud-messaging firebase-notifications