FCM - Not registered

17,034

Solution 1

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.

Solution 2

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.

Share:
17,034
Jach Michael
Author by

Jach Michael

Updated on June 24, 2022

Comments

  • Jach Michael
    Jach Michael almost 2 years

    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

  • Jach Michael
    Jach Michael over 6 years
    The key is not on 2 lines. It's just because my screen is too small that is appear like that. But in fact it's only a single line, so It can't be the problem. So I guess my problem is that my app is not registered... How can I do that ?