【问题标题】:FCM notification for android适用于 Android 的 FCM 通知
【发布时间】:2016-08-26 18:42:24
【问题描述】:

我正在尝试使用 PHP 脚本发送 FCM 通知,在数据库插入事件中,我尝试从 FCM 控制台发送,它使用主题选项运行良好,因为我知道我将应用程序实例注册到某个主题“exptopic”使用MainActivity.java OnCreate 类中的以下代码:

FirebaseMessaging.getInstance().subscribeToTopic("exptopic");

现在PHP脚本如下:

function sendMessage($data,$target){
    //FCM api URL
    $url = 'https://fcm.googleapis.com/fcm/send';

    //api_key available in Firebase console -> project settings -> cloud messaging -> server key

    $server_key = "API_KEY";//my api key

    $fields = array();
    $fields['data'] = array('message'=>$data);
    $fields['to'] = $target;

    $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, json_encode($fields));
    $result = curl_exec($ch);

    if($result === FALSE){
        die('FCM Send error: '.curl_error($ch));
        echo 'notification not sent';
    }
    curl_close($ch);
    echo $result;
    return $result;
}

显然$target = "exptopic",当我运行 snedMessage 函数时,它返回以下结果:

{"multicast_id":6779996340041741184,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

有人可以帮忙吗?

【问题讨论】:

  • MySQL 似乎与此问题无关,已删除标记。

标签: php android firebase firebase-cloud-messaging


【解决方案1】:

基于this documentationto 字段应为/topics/exptopic

【讨论】:

    【解决方案2】:

    您在数据中缺少priority 字段。试试这个:

    $fields["priority"] = "high" //This is required
    

    编辑:

    我在这个身体上取得了成功。也许试试这个:

    <?php
    
    // Get cURL resource
    $ch = curl_init();
    
    // Set url
    curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
    
    // Set method
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    
    // Set options
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    // Set headers
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
      "Authorization: key=API_KEY",
      "Content-Type: application/json",
     ]
    );
    // Create body
    $json_array = [
            "notification" => [
                "title" => "TEST",
                "sound" => "default",
                "body" => $data
            ],
            "to" => $target,
            "priority" => "high"
        ]; 
    $body = json_encode($json_array);
    
    // Set body
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    
    // Send the request & save response to $resp
    $resp = curl_exec($ch);
    
    if(!$resp) {
      die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
    } else {
      echo "Response HTTP Status Code : " . curl_getinfo($ch,     CURLINFO_HTTP_CODE);
      echo "\nResponse HTTP Body : " . $resp;
    }
    
    // Close request to clear up some resources
    curl_close($ch);
    

    【讨论】:

    • 感谢您的回复,但它返回了同样的错误,我认为不是这个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    相关资源
    最近更新 更多