1

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

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

然后eclipse在按ctrl+shift+o时不会导入任何东西。

如何使用这个类?

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

4

2 回答 2

2

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

将此存储库添加到您的 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 服务器实现文档中有一个小示例。


更新

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

发件人.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;
}
于 2015-06-09T22:06:50.293 回答
1

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

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

于 2013-08-25T19:04:38.340 回答