gpt4 book ai didi

android - 如何导入发件人类 - Java App Engine

转载 作者:行者123 更新时间:2023-11-29 17:57:21 27 4
gpt4 key购买 nike

我正在尝试在我的网络应用程序中实现 Google 云消息传递。但是当我写这个时:

Sender sender = new Sender("My API Key");

然后 eclipse 不会在按下 ctrl+shift+o 时导入任何内容。

如何使用这个类?

http://developer.android.com/reference/com/google/android/gcm/server/Sender.html

最佳答案

将 Maven 用于我的 java 项目(并且由于我无法从之前的响应中找到引用),这是我必须做的(如 this github GCM repo for maven 中所述)。

将此存储库添加到您的 maven pom.xml:

<repositories>
. . .
<repository>
<id>gcm-server-repository</id>
<url>https://raw.githubusercontent.com/slorber/gcm-server-repository/master/releases/</url>
</repository>
</repositories>

然后添加这个依赖:

<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.*;

要使用它,有一个迷你示例 here在 android GCM 服务器实现文档中。


更新

由于示例链接已损坏,我在这里留下一个简短的示例,我使用 2 个业务类和 2 个(或更多)模型来管理响应和消息:

Sender.java

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);
}

GCMREsponseValidator.java

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;
}

其中 resonseResume 是这个模型:GCMResponseMResume

public class GCMResponseResume {
private MulticastResult multicastResult;
private int updatedCanonicalIdsTotal;
private int deletedIdsTotal;
}

关于android - 如何导入发件人类 - Java App Engine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18431516/

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