AleXoTroN
AleXoTroN

Reputation: 27

Firebase Cloud Messaging - invalidRegistration (php)

i have trouble with sending push notifications to topics in my Firebase project. If i run the php script, i gives me the error 'invalidRegistration'

php script:

<?php


$topic = "/topics/info";
$fcm_server_url = "https://fcm.googleapis.com/fcm/send";

$title = utf8_encode("AleXoTroN");
$content_text = utf8_encode("AleXoTroN");
$color_code = utf8_encode("#2baa7f");


$httpheader = array('Content_Type:application/json', 'Authorization:key=***');
$msg = array('to' => $topic, 'data' => array('title' => $title, 'content_text' => $content_text, 'color' => $color_code));

$curl_connection = curl_init();
curl_setopt($curl_connection, CURLOPT_URL, $fcm_server_url);
curl_setopt($curl_connection, CURLOPT_POST, true);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $httpheader);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $msg);
$answerFromServer = curl_exec($curl_connection);
curl_close($curl_connection);

echo "Server:<br/>".$answerFromServer;

?>

And the code of the FCM Instance ID Service in my application;

public class FCM_IIDS extends FirebaseInstanceIdService {

    final String token_preference_key = "fcm_token";

    final static String infoTopicName = "info";

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPreferences.edit().putString(token_preference_key, FirebaseInstanceId.getInstance().getToken()).apply();


        FirebaseMessaging.getInstance().subscribeToTopic(infoTopicName);
    }
}

Upvotes: 0

Views: 1644

Answers (1)

Sebastian Tkaczyk
Sebastian Tkaczyk

Reputation: 372

You aren't encoding to json your message.

Use json_encode($msg) before sending it to FCM.

Upvotes: 1

Related Questions