gpt4 book ai didi

spring - 如何使用 Spring Boot 发送通知 (FCM)

转载 作者:行者123 更新时间:2023-12-01 15:15:20 28 4
gpt4 key购买 nike

我有一个 API,当我更改名为 Meeting 的表中的某些内容时,我想向智能手机发送通知。为此,我正在创建一个 POST 类型的方法,我在其中发送以下 JSON 正文 还有授权 key (由 firebase 提供)和以下 URL https://fcm.googleapis.com/fcm/send .

@Headers -> *******



{
"to": "/topics/groupNameChoosenByYou",
"data": {
"title": "Your title",
"message": "Your message"
}
}

这是我的 AndroidPushNotificationsService
@Service
public class AndroidPushNotificationsService {

private static final String FIREBASE_SERVER_KEY = "AAAAq88vWWE:APA91bF5rSyqGbx26AY5jm6NsEfwJynQsnTd1MPFOTOz1ekTGZyof3Vz6gBb0769MLLxD7EXMcqKiPIHnqLh5buHEeUASnpsn-ltxR1J8z3kWJIAPNWlPZB0r0zKkXMyWkrbT3BLPWmCdi-NZnP3Jkb2z-QqtwJt5Q";
private static final String FIREBASE_API_URL = "https://fcm.googleapis.com/fcm/send";

@Async
public CompletableFuture<String> send(HttpEntity<String> entity) {

RestTemplate restTemplate = new RestTemplate();

ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(new HeaderRequestInterceptor("Authorization", "key=" + FIREBASE_SERVER_KEY));
interceptors.add(new HeaderRequestInterceptor("Content-Type", "application/json"));
restTemplate.setInterceptors(interceptors);

String firebaseResponse = restTemplate.postForObject(FIREBASE_API_URL, entity, String.class);

return CompletableFuture.completedFuture(firebaseResponse);
}
}

这是我的 HeaderRequestInterceptor
public class HeaderRequestInterceptor implements ClientHttpRequestInterceptor {

private final String headerName;
private final String headerValue;

public HeaderRequestInterceptor(String headerName, String headerValue) {
this.headerName = headerName;
this.headerValue = headerValue;
}

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
HttpRequest wrapper = new HttpRequestWrapper(request);
wrapper.getHeaders().set(headerName, headerValue);
return execution.execute(wrapper, body);
}
}

这是我的 Controller
@RestController
public class WebController {

private final String TOPIC = "test";

@Autowired
AndroidPushNotificationsService androidPushNotificationsService;

@RequestMapping(value = "/send", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<String> send() throws JSONException {

JSONObject body = new JSONObject();
body.put("to", "/topics/" + TOPIC);
body.put("priority", "high");

JSONObject notification = new JSONObject();
notification.put("title", "JSA Notification");
notification.put("body", "Happy Message!");

JSONObject data = new JSONObject();
data.put("Key-1", "JSA Data 1");
data.put("Key-2", "JSA Data 2");

body.put("notification", notification);
body.put("data", data);

HttpEntity<String> request = new HttpEntity<>(body.toString());

CompletableFuture<String> pushNotification = androidPushNotificationsService.send(request);
CompletableFuture.allOf(pushNotification).join();

try {
String firebaseResponse = pushNotification.get();

return new ResponseEntity<>(firebaseResponse, HttpStatus.OK);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}

return new ResponseEntity<>("Push Notification ERROR!", HttpStatus.BAD_REQUEST);
}
}

问题是当我去 http://localhost:8080/springbootapi/send我收到 404,但服务器正在运行。我想知道我拥有的代码是否一切正常,因为 FCM api 需要一个 POST 方法,并且在我的/send 方法中我正在执行 GET,所以基本上我正在创建一个 API 来调用其他 api 以提供通知主要目的是在更改表时向用户发送。有没有更好的方法来做到这一点,我们甚至是一个很好的做法?

最佳答案

FCM 需要 POST 并且您在 restTemplate 中发送 POST。您的 Controller 是 GET 还是 DELETE 都没有关系。
但是ofc你应该让它POST,因为你正在发送一些东西,而不是检索。
404 表示您输入了错误的 URL。它应该是:
http://localhost:8080/send (如果您使用的是 8080 端口)

关于spring - 如何使用 Spring Boot 发送通知 (FCM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51944285/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com