android - PUSH Notifications for >1000 devices through GCM Server(Java) -
i have gcm-backend java server , i'm trying send users notification msg. approach right? split them 1000 each time before giving send request? or there better approach?
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(); { list<registrationrecord> regidsparts = regidtrim(records, msgsdone); msgsdone+=1000; sendmsg(regidsparts,message); }while(msgsdone<count); } }
the regidtrim method
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; }
the sendmsg method
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 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) + "[...]"; } (registrationrecord record : records) { result result = sender.send(msg, record.getregid(), 5); if (result.getmessageid() != null) { log.info("message sent " + record.getregid()); string canonicalregid = result.getcanonicalregistrationid(); if (canonicalregid != null) { // if regid changed, have update datastore log.info("registration id changed " + record.getregid() + " updating " + 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 gcm, removing datastore"); // if device no longer registered gcm, remove datastore ofy().delete().entity(record).now(); } else { log.warning("error when sending message : " + error); } } } }
we can not send more 1000 push notification @ time.i searched lot not result did same approach split whole list in sub lists of 1000 items , send push notification.
Comments
Post a Comment