【问题标题】:iOS Swift - Is it possible to send notifications using FCM directly from PHP?iOS Swift - 是否可以直接从 PHP 使用 FCM 发送通知?
【发布时间】: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


【解决方案1】:

要通过 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 变量进行必要的更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    相关资源
    最近更新 更多