Wai Yan Hein Wai Yan Hein - 1 year ago 1799
Android Question

InvalidRegistration error in Firebase Cloud Messaging for Android

I am developing an Android App that using Push Notification feature. I need to push from server. I use Firebase for it. Frankly, this is my first time of using Firebase. I am newbie to Firebase. But when I push from server using PHP and CURL, it is giving me invalid registration error. Please see what I have done below.

I get the firebase token in android like this

String token = FirebaseInstanceId.getInstance().getToken();

Then I save sent that token to server and saved in the database.

At server, I am pushing like this

class Pusher extends REST_Controller {

function __construct()

public function notification_get()
$rows = $this->db->get('device_registration')->result();
$tokens= array();
foreach($rows as $row)
$tokens[] = $row->token;
$message = array("message"=>"FCM PUSH NOTIFICATION TESTING");
$result = $this->send_notification($tokens,$message);
die("Unable to send");
$this->response($result, REST_Controller::HTTP_OK);


function send_notification($tokens,$message)
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(

$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);
return FALSE;
return $result;

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.
enter image description here

As you can see it is giving InvalidRegistration error and message is not pushed to devices. So please what is wrong with my code? How can I fix it please? Please help me.


This is my FirebaseMessagingService class that show notification in android

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

public void onMessageReceived(RemoteMessage remoteMessage) {

private void showNotification(String message)
Intent i = new Intent(this,MainActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setAutoCancel(true)
.setContentTitle("FCM Test")

NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Answer Source

Invalid Registration ID Check the formatting of the registration ID that you pass to the server. Make sure it matches the registration ID the phone receives in the com.google.firebase.INSTANCE_ID_EVENT intent and that you're not truncating it or adding additional characters. Happens when error code is InvalidRegistration.

Please check with both the side app side and your side that the exact same registration id is stored in the server which Application on mobile receives it in on onTokenRefresh method. You should have received the exact same registration token as developer got in FirebaseInstanceId.getInstance().getToken()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download