Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM - Not registered

I'm trying to send a message through FCM to my web app. Unfortunately, I've the following error message and I'm unable to solve it alone...

Through POSTMAN I receive that JSON :

 {
    "multicast_id": 7441926471010389687,
    "success": 0,
    "failure": 1,
    "canonical_ids": 0,
    "results": [
        {
            "error": "NotRegistered"
        }
    ]
}

This is the only error message I have. See bellow POSTMAN config POST message I send to FCMenter image description here

enter image description here

Is someone have already facing the same problem ? I unable to find any response to solve my problem on internet...

See also bellow my Firebase.java

package com.inovans.backend.web.rest.firebase;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;

@Service
public class Firebase {

    @PostConstruct
    void initialierMyFirebase() {
        this.initializeFirebase();
    }

    private void initializeFirebase() {

        // if(this.firebaseApp.getApps().isEmpty()) {

        FileInputStream serviceAccount = null;
        try {
            serviceAccount = new FileInputStream(
                    "src/main/java/com/inovans/backend/web/rest/firebase/emergency-manager-57773-firebase-adminsdk-x4p6o-c2ea07c8f4.json");
        } catch (FileNotFoundException e) {
            System.out.println(
                    "Impossible de récupérer le fichier JSON de configuration de FIREBASE. Le chemin n'est pas valide. "
                            + serviceAccount);
            e.printStackTrace();
        }

        FirebaseOptions options = null;
        try {
            options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(serviceAccount))
                    .setDatabaseUrl("https://emergency-manager-57773.firebaseio.com").build();
        } catch (IOException e) {
            System.out.println("Impossible d'initialiser l'application FIREBASE : " + options);
            e.printStackTrace();
        }

        System.out.println("OPT : " + options);

        FirebaseApp.initializeApp(options);

        // }
    }

    public void sendMessage(String token) {

        // See documentation on defining a message payload.
        /*
        Message message = Message.builder()
                .setWebpushConfig(WebpushConfig.builder()
                        .setNotification(new WebpushNotification("$GOOG up 1.43% on the day",
                                "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
                                "https://my-server/icon.png"))
                        .build())
                .setToken(
                        "fh4Jcwe3vhg:APA91bGvw6crsojSroBE31aeR32ZRjfJyCisHNUWiR6froP53c0YpQ7uG-EtkiPIQ0ZUnY2fYW_TF4T6NFhQ7W002Q2MBW8-4ONedruIWMpw8BXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                .build();
        */

        // See documentation on defining a message payload.
        Message message = Message.builder()
                .putData("score", "850")
                .putData("time", "2:45")
                .setToken("fh4Jcwe3vhg:APA91bGvw6crsojSroBE31aeR32ZRjfJyCisHNUWiR6froP53c0YpQ7uG-EtkiPIQ0ZUnY2fYW_TF4T6NFhQ7W002Q2MBW8-4ONedruIWMpw8BXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                .build();


        // Send a message to the device corresponding to the provided
        // registration token.
        String response = null;
        try {
            System.out.println("TEST : " + FirebaseMessaging.getInstance());
            response = FirebaseMessaging.getInstance().sendAsync(message).get();
            // Response is a message ID string.
            System.out.println("Successfully sent message: " + response);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Help please

like image 991
Jach Michael Avatar asked Feb 20 '18 11:02

Jach Michael


2 Answers

The official firebase documentation says:

An existing registration token may cease to be valid in a number of scenarios, including:

  • If the client app unregisters with FCM.
  • If the client app is automatically unregistered, which can happen if the user uninstalls the application. For example, on iOS, if the APNS Feedback Service reported the APNS token as invalid.
  • If the registration token expires (for example, Google might decide to refresh registration tokens, or the APNS token has expired for iOS devices).
  • If the client app is updated but the new version is not configured to receive messages.

For all these cases, remove this registration token from the app server and stop using it to send messages.

Usually you will receive this error in case when backend application sends a push with an expired token.

like image 103
timic Avatar answered Oct 13 '22 16:10

timic


The error NotRegistered means that your web app is not registered or the key you are using in the field "to" is not correct. In your case I suspect of the fact that the key is written in 2 lines, and maybe the backspace is being send in the request. Try to send the "to" field key in a single line.

like image 2
moictab Avatar answered Oct 13 '22 14:10

moictab