Android 如何导入发件人类-Java应用程序引擎
我正在尝试在我的web应用程序中实现Google云消息传递。 但当我写这篇文章时:Android 如何导入发件人类-Java应用程序引擎,android,google-app-engine,push-notification,google-cloud-messaging,Android,Google App Engine,Push Notification,Google Cloud Messaging,我正在尝试在我的web应用程序中实现Google云消息传递。 但当我写这篇文章时: Sender sender = new Sender("My API Key"); 然后按ctrl+shift+o时eclipse不会导入任何内容 如何使用这个类 您应该将gcmserver.jar(位于\extras\google\gcm\gcmserver\dist\gcmserver.jar)添加到eclipse项目中 您可以通过右键单击项目并选择Properties->Java Build Path->
Sender sender = new Sender("My API Key");
然后按ctrl+shift+o时eclipse不会导入任何内容
如何使用这个类
您应该将
gcmserver.jar
(位于\extras\google\gcm\gcmserver\dist\gcmserver.jar
)添加到eclipse项目中
您可以通过右键单击项目并选择
Properties->Java Build Path->Libraries->Add External JARs…
使用Maven进行我的Java项目(由于我无法从前面的响应中找到引用),下面是我必须做的事情(如中所述)
将此存储库添加到maven pom.xml:
<repositories>
. . .
<repository>
<id>gcm-server-repository</id>
<url>https://raw.githubusercontent.com/slorber/gcm-server-repository/master/releases/</url>
</repository>
</repositories>
要使用它,android GCM服务器实现文档中有一个小示例
更新 由于示例链接已断开,我在此留下一个简短的示例,在此示例中,我使用2个业务类别和2个(或更多)模型来管理响应和消息: Sender.java GCMREsponseValidator.java
我在我的android sdk文件夹中找不到gcm-server.jar。@omerjerk您必须安装android库的Google Cloud Messaging(转到android sdk管理器-您会在Extras下方找到它)。不推荐的写在那里。。这就是我没有安装它的原因。@omerjerk客户端库已被弃用(尽管它仍然有效),但服务器库却没有。Extras下没有Google云消息。我已经下载了adt-bundle-windows-x86_64-20131030
<dependencies>
. . .
<dependency>
<groupId>com.google.android.gcm</groupId>
<artifactId>gcm-server</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
import com.google.android.gcm.server.*;
public MulticastResult SendMessage(YourMessageObject yourMsg) throws IOException {
Sender sender = new Sender(gcmApiKey); // A string
String msg = yourMsg.getMessage();
String msgTxt = (msg.isEmpty()) ? "This is a test" : msg ;
Message message = new Message.Builder()
.addData("title", "My title")
.addData("message", msgTxt)
.addData("msgCount", "3")
.addData("jsonEvent", yourMsg.getEventJson())
.build();
if(devices.isEmpty()){
return null;
}
return sender.send(message, devices, 5);
}
public GCMResponseResume validateResponse(MulticastResult multicastResult, List<GCMClient> devices){
String response;
response = "multicast: " + multicastResult.toString();
if(multicastResult.getMulticastId() != 0){
responseResume = new GCMResponseResume(multicastResult);
// We check the results
int index = 0;
List<Result> results = multicastResult.getResults();
for(final Result result : results){
// Si responde exitosamente
if(result.getMessageId() != null){
// If there is a canonical Id we replace the Id
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
response = response.concat("\nCanonical found: " + canonicalRegId);
response = response.concat("\n... updating: " + devices.get(index));
Optional<GCMClient> currentDevice = Optional.of(devices.get(index));
String updated = clientsDAO.updateRegId(currentDevice, canonicalRegId);
response = response.concat("\n... updated: " + updated);
responseResume.addUpdatedIdsTotal(Integer.parseInt(updated));
}
}
// If not a successful result
else {
String error = result.getErrorCodeName();
// Y tiene error de no registrado, borramos el id correspondiente
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
String notRegisteredId = devices.get(index).getRegisterId();
response = response.concat("\nNot Registered found: " + notRegisteredId);
response = response.concat("\n... deleting: " + notRegisteredId);
int removed = clientsDAO.removeByRegId(notRegisteredId);
response = response.concat("\n... deleted:" + Integer.toString(removed));
responseResume.addDeletedIdsTotal(removed);
}
}
index++;
}
response = response.concat("\nResults: " + results.toString());
}
return responseResume;
}
public class GCMResponseResume {
private MulticastResult multicastResult;
private int updatedCanonicalIdsTotal;
private int deletedIdsTotal;
}