java - App Engine 后端与 Google Cloud Messaging 向 1000 多名用户发送消息

标签 java google-app-engine google-cloud-messaging sendmessage multiple-users

我想向所有用户(~15,000)发送一条消息(例如可用更新)。我已经实现了App Engine Backend with Google Cloud Messaging发送消息。

我已经在 2 台设备上进行了测试。双方都收到消息了但正如谷歌文档所述“GCM 支持单条消息最多 1,000 个收件人。”

My question is how to send same message to remaining 14,000 users in my case? Or the code below will take care of it?

下面是发送消息的代码

import com.google.android.gcm.server.Constants;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiNamespace;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.inject.Named;

import static com.example.shani.myapplication.backend.OfyService.ofy;

/**
 * An endpoint to send messages to devices registered with the backend
 * <p/>
 * For more information, see
 * https://developers.google.com/appengine/docs/java/endpoints/
 * <p/>
 * NOTE: This endpoint does not use any form of authorization or
 * authentication! If this app is deployed, anyone can access this endpoint! If
 * you'd like to add authentication, take a look at the documentation.
 */
@Api(name = "messaging", version = "v1", namespace = @ApiNamespace(ownerDomain = "backend.myapplication.shani.example.com", ownerName = "backend.myapplication.shani.example.com", packagePath = ""))
public class MessagingEndpoint {
    private static final Logger log = Logger.getLogger(MessagingEndpoint.class.getName());

    /**
     * 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(1000).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);
                }
            }
        }
    }
}

我知道有类似的问题,但我使用的是 Java 语言。我发现后端使用php语言的问题。所以对我没有帮助!

  1. Google Cloud Messaging: Send message to "all" users
  2. Sending Push Notification on multiple devices

有人成功实现过App Engine+Google Cloud Messaging JAVA语言吗?

在下面的代码行中,如果我将 1000 替换为 15,000 会解决我的问题吗?

List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(1000).list();

请尽快提供帮助。非常抱歉我的英语。如果有人需要其他详细信息,欢迎询问。

感谢您的宝贵时间。

最佳答案

一些注意事项,

1) 向可能大量的用户发送通知可能会花费大量时间,请考虑使用 Task Queues将要在 60 秒限制之外“离线”完成的工作排队。

2) 现在至于 GCM 限制,如果您需要所有用户,但 GCM 允许您一次 1000 个,只需将它们分成 1000 个批处理,并为每个批处理单独发送一条消息。

如果您结合这两个建议,您应该拥有一个相当可扩展的流程,您可以在 1 个请求中查询所有用户,拆分该列表,然后排队将消息一次发送给这些用户 1000 个。

关于java - App Engine 后端与 Google Cloud Messaging 向 1000 多名用户发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29716467/

相关文章:

java - 在所有递归调用从调用堆栈中弹出之前,递归函数返回最终结果

java - Retrofit 中的自定义超时消息

python - i18n 与 jinja2 + GAE

security - 使用 Google App Engine JDK 在版本控制之外管理安全配置

android - Google API 中的server-key、android-key、browser-key 和iOS key 有什么区别?

java - 通过android中的接口(interface)将gcm注册ID从GCMBaseIntentService类传递到MainActivity

java - java中的邻接矩阵,广度优先搜索

java - 带撇号的单词的正则表达式 (Java)

python - 如何确定您的应用程序是否在本地 Python 开发服务器上运行?

java - 使用此代码 (PHP) 向多个设备发送推送通知