iOS Swift - 是否可以直接从 PHP 使用 FCM 发送通知?
iOS Swift - Is it possible to send notifications using FCM directly from PHP?
我使用 FCM 向 Android 和 PHP 发送推送通知。这是我的代码:
<?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 发送了 individual/group/topic 通知并且效果很好。为什么从 file.php
起不起作用?
帮助?!
要通过 FCM 发送 iOS,您需要在有效负载中定义 notification 键。它在文档中有解释,请在那里查看。 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 变量进行必要的更改。
我使用 FCM 向 Android 和 PHP 发送推送通知。这是我的代码:
<?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 发送了 individual/group/topic 通知并且效果很好。为什么从 file.php
起不起作用?
帮助?!
要通过 FCM 发送 iOS,您需要在有效负载中定义 notification 键。它在文档中有解释,请在那里查看。 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 变量进行必要的更改。