Java FCM未看到从spring后端应用发送的消息
我在Firebase Cloud Messaging下配置了一个android应用程序,当我从控制台发送通知时,我会在android应用程序中看到通知 我现在正试图实现从后端服务器java spring应用程序发送通知,下面是我的服务:Java FCM未看到从spring后端应用发送的消息,java,android,spring,firebase,spring-boot,Java,Android,Spring,Firebase,Spring Boot,我在Firebase Cloud Messaging下配置了一个android应用程序,当我从控制台发送通知时,我会在android应用程序中看到通知 我现在正试图实现从后端服务器java spring应用程序发送通知,下面是我的服务: @Slf4j @Service public class FCMService { public void sendMessageWithData() throws FirebaseMessagingException { Messa
@Slf4j
@Service
public class FCMService {
public void sendMessageWithData() throws FirebaseMessagingException {
Message message = getPreconfiguredMessageWithData();
String response = sendAndGetResponse(message);
log.info("Sent message with data.");
}
private String sendAndGetResponse(Message message) throws FirebaseMessagingException {
return FirebaseMessaging.getInstance().send(message);
}
private AndroidConfig getAndroidConfig(String topic) {
return AndroidConfig.builder()
.setTtl(Duration.ofMinutes(2).toMillis()).setCollapseKey(topic)
.setPriority(AndroidConfig.Priority.HIGH)
.setNotification(AndroidNotification.builder().setSound("default")
.setColor("#FFFF00").setTag(topic).build()).build();
}
private ApnsConfig getApnsConfig(String topic) {
return ApnsConfig.builder()
.setAps(Aps.builder().setCategory(topic).setThreadId(topic).build()).build();
}
private Message getPreconfiguredMessageWithData() {
return getPreconfiguredMessageBuilder().setTopic("all")
.putData("test", "data")
.build();
}
private Message.Builder getPreconfiguredMessageBuilder() {
AndroidConfig androidConfig = getAndroidConfig("all");
ApnsConfig apnsConfig = getApnsConfig("all");
return Message.builder()
.setApnsConfig(apnsConfig).setAndroidConfig(androidConfig)
.setNotification(
new Notification("sample title", "sample message"));
}
}
消息已正确发送,我在响应中获得消息的id,但该通知在应用程序中不可见,并且我无法在firebase控制台中将其视为新通知。问题在哪里?我还缺少什么吗?你可以用curl测试你的应用程序发送推送通知,这样你就可以验证问题是在你的服务中还是在你的应用程序中
curl -X POST -H "Authorization: Bearer YOUR-TOKEN" -H "Content-Type: application/json" -d '{
"message":{
"token":"TARGET_DEVICE_TOKEN",
"notification":{
"title":"Hello",
"body":"This is a text message!"
}
}
}' https://fcm.googleapis.com/v1/projects/YOUR-PROJECT-SHORT-NAME/messages:send
我是否总是需要设备令牌?我如何向firebase项目中的所有设备发送通知?有很多方法可以处理此问题,例如在数据库中保存每个设备的令牌,有一种方法可以向所有用户发送推送通知,在这种情况下不需要特定令牌检查:谢谢,我还有一个问题:当我在服务器端初始化我的应用程序,然后获取firebase应用程序实例时,我得到一个名为DEFAUL的应用程序,你知道为什么会发生这种情况吗?我不明白,你在哪里可以看到这种默认情况?