我有一个gcm后端java服务器,我试图向所有用户发送一条通知消息。我的方法对吗?在发出发送请求之前每次将它们分成1000个?还是有更好的方法?

    public void sendMessage(@Named("message") String message) throws IOException {

    int count = ofy().load().type(RegistrationRecord.class).count();

    if(count<=1000) {
        List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(count).list();
        sendMsg(records,message);
    }else
    {
        int msgsDone=0;
        List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).list();

        do {
            List<RegistrationRecord> regIdsParts = regIdTrim(records, msgsDone);
            msgsDone+=1000;
            sendMsg(regIdsParts,message);
        }while(msgsDone<count);

      }
   }

regidtrim方法
   private List<RegistrationRecord> regIdTrim(List<RegistrationRecord> wholeList, final int start) {

    List<RegistrationRecord> parts = wholeList.subList(start,(start+1000)> wholeList.size()? wholeList.size() : start+1000);
    return parts;
}

sendmsg方法
    private void sendMsg(List<RegistrationRecord> records,@Named("message") String message) throws IOException {

    if (message == null || message.trim().length() == 0) {
        log.warning("Not sending message because it is empty");
        return;
    }

    Sender sender = new Sender(API_KEY);
    Message msg = new Message.Builder().addData("message", message).build();

    // crop longer messages
    if (message.length() > 1000) {
        message = message.substring(0, 1000) + "[...]";
    }
    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);
            }
        }
    }
}

最佳答案

引用自Google Docs
GCM支持单个邮件最多1000个收件人。此功能使向整个用户群发送重要消息更加容易。例如,假设您有一条消息需要发送给1000000个用户,而您的服务器每秒可以处理发送大约500条消息。如果您只使用一个收件人发送每封邮件,则需要1000000/500=2000秒,或大约半小时。但是,将1000个收件人附加到每个邮件后,向1000000个收件人发送邮件所需的总时间将变为(1000000/1000)/500=2秒。这不仅有用,而且对于及时的数据也很重要,例如自然灾害警报或运动成绩,在这些数据中,30分钟的间隔可能会使信息变得无用。
利用这个功能很容易。如果您使用的是gcmhelper library for Java,只需向send或sendnority方法提供注册id的列表集合,而不是单个注册id。

关于java - 通过GCM Server(Java)超过1000个设备的PUSH通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29934331/

10-10 20:24