我正在使用FCM向MySSQL和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
?
帮助?!
答案 0 :(得分:1)
要通过FCM发送iOS,您需要在有效负载中定义通知键。它在文档中解释请看一下。 https://firebase.google.com/docs/cloud-messaging/ios/send-multiple
这是一个示例请求
{
"condition": "'all' in topics",
"priority" : "high",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message Title",
}
}
在上面的示例中,我将发送名为 all 的主题。您应该创建主题并将用户订阅到相关主题。 您需要在$ fields变量中进行必要的更改。