我正在尝试通过 FCM 向我的网络应用程序发送消息。不幸的是,我有以下错误消息,我无法单独解决...
通过 POSTMAN 我收到了那个 JSON:
{
"multicast_id": 7441926471010389687,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "NotRegistered"
}
]
}
这是我收到的唯一错误消息。请参阅下面的 POSTMAN 配置 POST 消息我发送给 FCM
是否有人已经面临同样的问题?我无法在 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();
}
}
}
请帮忙
最佳答案
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/