PHP Firebase Clood Messaging(FCM)错误缺少注册

时间:2017-08-14 14:38:49

标签: php android firebase firebase-cloud-messaging

我正在建立从服务器到FCM服务器的推送通知。我收到以下错误:Error=MissingRegistration

我的PHP代码如下所示。

function send($id,$title,$text){
  $msg = [
    'title' => $title,
    'body' => $text,
    'icon' => 'myicon',
     'sound' => 'mySound'
  ];

  $fields = [
    'to' => $id,
    'notification' => $msg
  ];

  $headers = [
    'Authorization: key=' . $api_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_POSTFIELDS, $fields );
  $result = curl_exec ( $ch );
  curl_close ( $ch );
  echo $result;
}

我正在调用这样的函数:

send($id,$title,$text);

1 个答案:

答案 0 :(得分:3)

要将数据发送到FCM,您需要创建json数据。请在字段

之后添加此行
$fields = json_encode($fields);