我有一个API,我想将通知发送到智能手机时,我改变了一个名为会议桌上的东西。对于我创造型POST的方法,其中我送以下JSON机身采用也授权密钥(由火力提供)和到以下网址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);
}
}
这是我的控制器
@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
I收到了404,但服务器正在运行。我想知道,如果一切都确定了我的代码,因为FCM API需要一个POST方法,并在我的/发送方法,我做一个GET,所以基本上我创建了一个API来调用其他API,以提供通知用户在数据表被改变,这就是最主要的目的。有没有更好的办法做到这一点我们甚至有一个好的做法呢?
FCM需要POST和你在restTemplate发送POST。如果您的控制器是GET甚至删除它并不重要。但是OFC你应该让POST因为你送点东西,没有检索。 404意味着你正在输入错误的URL。它应该是:http://localhost:8080/send(如果你使用的是8080端口)
下面@RestController
添加注释@RequestMapping("/springbootapi")
或任何你期望的路径。
确保你没有任何上下文路径设置为开机春application(Check log and search for Context path which might defined in Application.yml/properties)
如果您收到404随后将端口8080是完全没有问题,但要确保该端口通过你的春天启动应用程序only(check the spring boot printed logs in console for port also)
打开。
我相信从你的问题是,404错误是不是FCM的,因为但随着应用程序,您在您的计算机上运行。