title: InvalidRegistration error in Firebase Cloud Messaging for Android
body: <p>I am developing an Android app that using Push Notification feature. I need to push from server. I use Firebase for it. This is my first time using Firebase. But when I push from server using PHP and CURL, it is giving me invalid registration error.</p>
  <p>I get the Firebase token in Android like this</p>
  <pre><code>String token = FirebaseInstanceId.getInstance().getToken();
  </code></pre>
  <p>Then I save sent that token to server and saved in the database.</p>
  <p>At server, I am pushing like this</p>
  <pre><code>class Pusher extends REST_Controller {
  
      function __construct()
      {
          parent::__construct();
      }
  
      public function notification_get()
      {
          $rows = $this-&gt;db-&gt;get('device_registration')-&gt;result();
          $tokens= array();
          if(count($rows)&gt;0)
          {
              foreach($rows as $row)
              {
                  $tokens[] = $row-&gt;token;
              }
          }
          $message = array(&quot;message&quot;=&gt;&quot;FCM PUSH NOTIFICATION TESTING&quot;);
          if(count($tokens)&gt;0)
          {
              $result = $this-&gt;send_notification($tokens,$message);
              if(!$result)
              {
                  die(&quot;Unable to send&quot;);
              }
              else{
                  $this-&gt;response($result, REST_Controller::HTTP_OK);
              }
          }
          
      }
  
      function send_notification($tokens,$message)
      {
          $url = 'https://fcm.googleapis.com/fcm/send';
          $fields = array(
                  'registration_ids'=&gt;$tokens,
                  'data'=&gt;$message
              );
  
          $headers = array(
                  'Authorization:key = AIzaSyApyfgXsNQ3dFTGWR6ns_9pttr694VDe5M',//Server key from firebase
                  '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)
          {
              return FALSE;
          }
          curl_close($ch);
          return $result;
      }
  }
  </code></pre>
  <p>I am using CodeIgniter 3 framework for building Rest API. When I push accessing URL from browser, it returns JSON data with error as in the below screenshot.</p>
  <p><a href="https://i.stack.imgur.com/uhHu6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uhHu6.png" alt="enter image description here" /></a></p>
  <p>As you can see it is giving InvalidRegistration error and message is not pushed to devices. What is wrong with my code?</p>
  <h2>Additional</h2>
  <p>This is my FirebaseMessagingService class that show notification in Android</p>
  <pre><code>public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
  
      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
          super.onMessageReceived(remoteMessage);
          showNotification(remoteMessage.getData().get(&quot;message&quot;));
      }
  
      private void showNotification(String message)
      {
          Intent i = new Intent(this,MainActivity.class);
          i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
          PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
  
          NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setAutoCancel(true)
                  .setContentTitle(&quot;FCM Test&quot;)
                  .setContentText(message)
                  .setSmallIcon(R.drawable.info)
                  .setContentIntent(pendingIntent);
  
          NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
          manager.notify(0,builder.build());
      }
  }
  </code></pre>