目前我正在 Firebase Messaging上编写一个简单的API 。我的代码看起来像这样 -
CODE
@Path("/f/n")
public class Notify {
public static final Logger logger = Logger.getLogger(Notify.class);
@Path("/m")
@Produces("application/json")
@GET
public String sendMessageMbl(@Context ServletContext context, @Context HttpServletRequest request, @Context HttpHeaders httpHeaders) throws Exception{
String response1 = "shreya";
response1=FCM(response1);
return response1;
}
public String FCM(String response1){
try {
String credential_file = PropertiesConstant.GOOGLE_SERVICES_JSON;
FileInputStream serviceAccount = new FileInputStream(credential_file);
FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(serviceAccount)).build();
try {
FirebaseApp.initializeApp(options);
}
catch(Exception e){
logger.error(e);
}
// This registration token comes from the client FCM SDKs.
String registrationToken = "XYZ";
Message message = Message.builder()
.putData("score", "850")
.putData("time", "2:45")
.setToken(registrationToken)
.build();
response1 = FirebaseMessaging.getInstance().sendAsync(message).get();
}catch(Exception e){
logger.error(e);
}
return response1;
}
}
错误消息
java.util.concurrent.ExecutionException:com.google.firebase.messaging.FirebaseMessagingException: 请求包含无效参数。
请求
获取http://localhost:8080/rest/f/n/m
REFERNCE
https://firebase.google.com/docs/cloud-messaging/admin/send-messages#send_to_individual_devices