Der Fehler, den ich erhalten habe, ist:
Fehler beim Senden von Benachrichtigungen: Unerwartet HTTP-Antwort mit Status: 404
404. Das ist ein Fehler.
Die angeforderte URL /batch wurde auf diesem Server nicht gefunden. Das ist alles, was wir wissen.
Unten ist der Code:
Code: Select all
public class FireBaseMessagingService {
private static final Logger log = LoggerFactory.getLogger(FireBaseMessagingService.class);
@Autowired
FirebaseMessaging firebaseMessaging;
public String sendNotifications(NotificationMessagingFCM notifications) {
String title = notifications.getTitle();
String body = notifications.getBody();
String image = notifications.getImage();
List recipientTokens = notifications.getRecipientToken();
Notification notification = Notification.builder().setTitle(title).setBody(body).setImage(image).build();
int batchSize = 500;
int totalSuccess = 0;
int totalFailure = 0;
for (int i = 0; i < recipientTokens.size(); i += batchSize) {
List batchTokens = recipientTokens.subList(i, Math.min(i + batchSize, recipientTokens.size()));
MulticastMessage message = MulticastMessage.builder()
.addAllTokens(batchTokens)
.setNotification(notification)
.build();
try {
BatchResponse response = firebaseMessaging.sendMulticast(message);
totalSuccess += response.getSuccessCount();
totalFailure += response.getFailureCount();
if (response.getFailureCount() > 0) {
List failures = response.getResponses();
for (int j = 0; j < failures.size(); j++) {
if (!failures.get(j).isSuccessful()) {
log.error("Failed to send notification to token: " + batchTokens.get(j) + " - "
+ failures.get(j).getException());
}
}
}
} catch (FirebaseMessagingException e) {
log.error("Error sending notifications: " + e.getMessage(), e);
return "Error Sending Notifications: " + e.getMessage();
}
}
return "Success: " + totalSuccess + " messages were sent, Failures: " + totalFailure;
}
}