如何导入Sender类 - Java App Engine

时间:2013-08-25 17:05:29

标签: android google-app-engine push-notification google-cloud-messaging

我正试图在我的网络应用程序中实施Google云消息传递。 但是当我写这篇文章时:

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

然后eclipse在按下ctrl + shift + o时没有导入任何东西。

如何使用这个课程?

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

2 个答案:

答案 0 :(得分:2)

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

要使用它,android GCM Server实施文档中有一个迷你示例here

<强>更新

由于示例链接已损坏,我在此处留下一个简短示例,其中我使用了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;
}

答案 1 :(得分:1)

您应该将gcm-server.jar(位于<Your Android SDK Dir>\extras\google\gcm\gcm-server\dist\gcm-server.jar)添加到您的日食项目中。

您可以通过右键单击项目并选择Properties -> Java Build Path -> Libraries -> Add External JARs...

来执行此操作