如何从 Google App Engine 应用程序发送 Firebase 云消息
How to send Firebase Cloud Message from Google App Engine App
如何从 Google 应用程序 Engine/Cloud 端点应用程序发送 Firebase 云消息?
Android Studio 自动生成以下代码来发送 Google 云消息。虽然您可以使用相同的代码发送 FCM,但您不能设置 "notification" 或 "priority" 或新 FCM 使用的任何类似代码。
是否有针对此的 gradle 导入或如何在 App Engine 应用程序中使用 Firebase 云消息传递的示例,以便您可以轻松地执行设置消息、通知、优先级等操作?
这是 Android Studio Cloud Endpoints 当前自动为您生成的内容:
// Gradle dependency:
compile 'com.google.gcm:gcm-server:1.0.0'
/**
* Api Keys can be obtained from the google cloud console
*/
private static final String API_KEY = System.getProperty("gcm.api.key");
/**
* Send to the first 10 devices (You can modify this to send to any number of devices or a specific device)
*
* @param message The message to send
*/
public void sendMessage(@Named("message") String message) throws IOException {
if (message == null || message.trim().length() == 0) {
log.warning("Not sending message because it is empty");
return;
}
// crop longer messages
if (message.length() > 1000) {
message = message.substring(0, 1000) + "[...]";
}
Sender sender = new Sender(API_KEY);
Message msg = new Message.Builder().addData("message", message).build();
List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(10).list();
for (RegistrationRecord record : records) {
Result result = sender.send(msg, record.getRegId(), 5);
if (result.getMessageId() != null) {
log.info("Message sent to " + record.getRegId());
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// if the regId changed, we have to update the datastore
log.info("Registration Id changed for " + record.getRegId() + " updating to " + canonicalRegId);
record.setRegId(canonicalRegId);
ofy().save().entity(record).now();
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
log.warning("Registration Id " + record.getRegId() + " no longer registered with GCM, removing from datastore");
// if the device is no longer registered with Gcm, remove it from the datastore
ofy().delete().entity(record).now();
} else {
log.warning("Error when sending message : " + error);
}
}
}
}
目前没有用于从应用服务器发送消息的 Firebase 客户端。您应该使用记录在案的协议从端点发送带有 JSON 负载的原始 HTTP 请求 here. The server reference 显示了您可以使用的参数,其中包括优先级。
如何从 Google 应用程序 Engine/Cloud 端点应用程序发送 Firebase 云消息?
Android Studio 自动生成以下代码来发送 Google 云消息。虽然您可以使用相同的代码发送 FCM,但您不能设置 "notification" 或 "priority" 或新 FCM 使用的任何类似代码。
是否有针对此的 gradle 导入或如何在 App Engine 应用程序中使用 Firebase 云消息传递的示例,以便您可以轻松地执行设置消息、通知、优先级等操作?
这是 Android Studio Cloud Endpoints 当前自动为您生成的内容:
// Gradle dependency:
compile 'com.google.gcm:gcm-server:1.0.0'
/**
* Api Keys can be obtained from the google cloud console
*/
private static final String API_KEY = System.getProperty("gcm.api.key");
/**
* Send to the first 10 devices (You can modify this to send to any number of devices or a specific device)
*
* @param message The message to send
*/
public void sendMessage(@Named("message") String message) throws IOException {
if (message == null || message.trim().length() == 0) {
log.warning("Not sending message because it is empty");
return;
}
// crop longer messages
if (message.length() > 1000) {
message = message.substring(0, 1000) + "[...]";
}
Sender sender = new Sender(API_KEY);
Message msg = new Message.Builder().addData("message", message).build();
List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(10).list();
for (RegistrationRecord record : records) {
Result result = sender.send(msg, record.getRegId(), 5);
if (result.getMessageId() != null) {
log.info("Message sent to " + record.getRegId());
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// if the regId changed, we have to update the datastore
log.info("Registration Id changed for " + record.getRegId() + " updating to " + canonicalRegId);
record.setRegId(canonicalRegId);
ofy().save().entity(record).now();
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
log.warning("Registration Id " + record.getRegId() + " no longer registered with GCM, removing from datastore");
// if the device is no longer registered with Gcm, remove it from the datastore
ofy().delete().entity(record).now();
} else {
log.warning("Error when sending message : " + error);
}
}
}
}
目前没有用于从应用服务器发送消息的 Firebase 客户端。您应该使用记录在案的协议从端点发送带有 JSON 负载的原始 HTTP 请求 here. The server reference 显示了您可以使用的参数,其中包括优先级。