java - FCM - 未注册

标签 java firebase firebase-cloud-messaging

我正在尝试通过 FCM 向我的网络应用程序发送消息。不幸的是,我有以下错误消息,我无法单独解决...

通过 POSTMAN 我收到了那个 JSON:

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

这是我收到的唯一错误消息。请参阅下面的 POSTMAN 配置 POST 消息我发送给 FCM enter image description here

enter image description here

是否有人已经面临同样的问题?我无法在 Internet 上找到任何解决方案来解决我的问题...

另见下面我的 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();
        }
    }

}

请帮忙

最佳答案

官方firebase documentation说:

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.

通常,当后端应用程序发送带有过期 token 的推送时,您会收到此错误。

关于java - FCM - 未注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48884050/

相关文章:

java - 使用表单条目填充电子邮件 Eclipse Android

java - Android 在 include 标签中找不到 BindingConversion

java - 在 PODAM 7.0.0 中创建递归对象时出现 PodamMockeryException

java - 使用 CSV2TableLayout 解析 CSV 但出现问号

ios - 尝试设置 Firebase 推送通知时出现错误

android - "ZipException: duplicate entry"集成 Firebase 时

swift - Xcode Swift Firebase Auth 使用电话号码和密码

ios - FCM 后台通知在 iOS 中不起作用

android - 从 OnePlus 3 中的最近应用程序列表中刷出应用程序时,未收到来自 FCM Firebase 云消息传递的任何通知

android - 如果满足某些条件,是否可以使用 firebase 创建自动推送通知,而无需网络?