alien
alien

Reputation: 214

PHP Firebase Clood Messaging (FCM) error missing registration

I am building a push notification from my server to FCM server. I getting following errors: Error=MissingRegistration

My PHP code is given below.

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;
}

I am calling function like this:

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

Upvotes: 3

Views: 2826

Answers (1)

Alien
Alien

Reputation: 724

For sending data to FCM you need to create json data. Please add this line after fields

$fields = json_encode($fields);

Upvotes: 3

Related Questions