【发布时间】:2020-12-04 13:49:17
【问题描述】:
好的,事情就是这样,我的同事已经将我们的后端应用注册到Firebase Console。我正在使用下面的代码向FCM 发送任何通知,从而向iOS 上的最终用户发送通知,但我没有这样做!
@RequestMapping(value = "/admin/invoice/message", method = RequestMethod.GET)
@ResponseBody
public void getMessage() {
try {
org.springframework.core.io.Resource resource = new ClassPathResource("name_of_the_json_file_below.json");
InputStream dbAsStream = resource.getInputStream(); // when spring boot project running as a jar
FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(dbAsStream))
.setDatabaseUrl("https://project_id_property.firebaseio.com").build();
@SuppressWarnings("unused")
FirebaseApp firebaseApp = null;
List<FirebaseApp> firebaseApps = FirebaseApp.getApps();
if (firebaseApps != null && !firebaseApps.isEmpty()) {
for (FirebaseApp app : firebaseApps) {
if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) {
firebaseApp = app;
}
}
} else {
firebaseApp = FirebaseApp.initializeApp(options);
}
Notification notification = new Notification("title", "body");
Message message = Message.builder().setNotification(notification).putData("title", "title").putData("body", "body")
.putData("type", "type").putData("sound", "default").putData("priority", "high").setToken("valid_push_token").build();
if (firebaseApp != null) System.out.println("aaa"); //aaa
String responseMessage = FirebaseMessaging.getInstance().send(message);
System.out.println(responseMessage); //this prints -> projects/project_id/messages/0:1607085339665987%fbe62b14fbe62b14
return;
}catch (Exception e) {
e.printStackTrace();
}
return;
}
在 Spring Boot 项目内的 resource 文件夹中有一个 json 文件,大概是在向 FCM 注册应用程序时获得的。
这个文件看起来像这样:
{
"type": "service_account",
"project_id": "project_id",
"private_key_id": "some string with private key id",
"private_key": "some long string with private key",
"client_email": "firebase-adminsdk-k202i@project_id.iam.gserviceaccount.com",
"client_id": "some client id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-9cgqi%40project_id_string_property.iam.gserviceaccount.com"
}
一旦有来自 iOS 移动应用程序的用户登录,User 表就会更新为新的有效推送令牌,这是简单登录请求的一部分。该推送令牌是在实际登录请求发送到我们的后端应用程序之前获得的。即,用户的移动应用程序向 FCM 发出请求,并拉取新的推送令牌。我猜这是通常的情况。
为了测试,我只需调用本地服务 (Swagger) localhost8080:appName/admin/invoice/message。
移动应用没有收到任何通知!
问题是所有其他请求体非常大的服务,以及一堆检查都能够发送通知。不幸的是,我无法调用它们,一堆案例和条件需要以某种方式重叠才能到达上面的代码行以发送推送通知!
假设所有配置都正确,如何在本地向最终用户发送通知?
【问题讨论】:
标签: ios spring-boot push-notification firebase-cloud-messaging