我正在尝试使用FCM从我的服务器发送Sping Boot 中生成的通知。我的客户是在Flutter上制作的。
@Service
public class ChatNotificationService implements IChatNotificationsService{
@Override
public void notify(User sender, User receiver) {
Message msg = Message.builder()
.setToken(receiver.getNotificationDeviceID())
.putData("New message", "You have received a message from @" + sender.getUsername())
.build();
try{
FirebaseMessaging.getInstance().send(msg);
}catch (Exception e){
//Handle exception...
}
}
}
问题是它显然工作正常。但是,消息永远不会到达设备。
消息构建无误,try
块也正确执行。它返回预期的ID sush,
projects/myproject-b5ae1/messages/0:1500415314455276%31bd1c9631bd1c96
但设备不会收到任何通知。
我已经尝试使用Firebase Jmeter 板手动向相同的设备注册令牌发送消息,它在那里工作正常。设备按预期接收通知。
它也可以正常使用 Postman 直接
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"body":"This is an FCM notification message!",
"title":"FCM Message"
}
}
}
这是我的配置类
@Configuration
public class FirebaseConfiguration {
@Bean
FirebaseMessaging firebaseMessaging(FirebaseApp firebaseApp) {
return FirebaseMessaging.getInstance(firebaseApp);
}
@Bean
FirebaseApp firebaseApp(GoogleCredentials credentials) {
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(credentials)
.build();
if(FirebaseApp.getApps().isEmpty()) {
return FirebaseApp.initializeApp(options);
}else{
return FirebaseApp.getInstance();
}
}
@Bean
GoogleCredentials googleCredentials() throws IOException {
return GoogleCredentials.fromStream(new ClassPathResource("firebase-key.json").getInputStream());
}
}
我试过的其他方法:
- 在仿真器和物理设备上测试
- 已打开防毒墙通知设置
1条答案
按热度按时间cfh9epnr1#
我假设你的配置是正确的,消息仍然需要一个
body
来发送。您可以通过Message.putData
添加body
、title
、image
。您输入的键New message
不被视为body
。试试这个还有,你可以这样做