Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为>;通过GCM服务器(Java)提供1000台设备_Java_Android_Google Cloud Messaging - Fatal编程技术网

为>;通过GCM服务器(Java)提供1000台设备

为>;通过GCM服务器(Java)提供1000台设备,java,android,google-cloud-messaging,Java,Android,Google Cloud Messaging,我有一个GCM后端Java服务器,我正在尝试向所有用户发送一个通知消息。我的方法正确吗?在发出发送请求之前,每次将它们拆分为1000个?还是有更好的方法 public void sendMessage(@Named("message") String message) throws IOException { int count = ofy().load().type(RegistrationRecord.class).count(); if(count<=10

我有一个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);

      }
   }
public void sendMessage(@Named(“message”)字符串消息)引发IOException{
int count=ofy().load().type(RegistrationRecord.class).count();
如果(数到1000){
message=message.substring(0,1000)+“[…]”;
}
用于(注册记录:记录){
结果=sender.send(msg,record.getRegId(),5);
if(result.getMessageId()!=null){
log.info(“消息发送到”+record.getRegId());
字符串canonicalRegId=result.getCanonicalRegistrationId();
如果(canonicalRegId!=null){
//如果regId发生了变化,我们必须更新数据存储
log.info(“将“+record.getRegId()+”的注册Id更改为“+canonicalRegId”);
record.setRegId(canonicalRegId);
ofy().save().entity(record).now();
}
}否则{
字符串错误=result.getErrorCodeName();
if(error.equals(Constants.error\u未注册)){
log.warning(“注册Id”+记录.getRegId()+“不再向GCM注册,从数据存储中删除”);
//如果设备不再向Gcm注册,请将其从数据存储中删除
ofy().delete().entity(record).now();
}否则{
日志警告(“发送消息时出错:+错误”);
}
}
}
}

我们一次发送的推送通知不能超过1000个。我搜索了很多,但没有找到结果,然后用相同的方法将整个列表拆分为1000个项目的子列表并发送推送通知。

引用自:

GCM支持单个邮件最多1000个收件人。此功能使向整个用户群发送重要消息变得更加容易。例如,假设您有一条消息需要发送给1000000个用户,而您的服务器可以处理每秒发送大约500条消息。如果您只使用一个收件人发送每条消息,则需要1000000/500=2000秒,或者大约半小时。但是,将1000个收件人附加到每条邮件时,向1000000个收件人发送邮件所需的总时间变为(1000000/1000)/500=2秒。这不仅有用,而且对于及时的数据(如自然灾害警报或体育成绩)非常重要,因为30分钟的间隔可能会使信息变得无用


利用此功能很容易。如果您使用的是GCM,只需向send或sendNoRetry方法提供注册ID的列表集合,而不是单个注册ID。

这是我的方式吗?如果是这样的话,它的效率有多高?您现在能在一个tym向1000多个用户发送消息吗?还是你找到了更好的解决办法?要发送消息,您使用的是api资源管理器还是简单的工具?我正在使用应用程序引擎后端。谢谢您的帮助。您好,我已经尝试过您的解决方案,但我可以向不到2000个用户发送gcm消息,但我有近20000多个用户。我不明白为什么这个代码不能完美地工作。你能告诉我你是否修改了密码吗。。请帮忙。
   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;
}
    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);
            }
        }
    }
}