Android GCM多搜索结果方法
这是我对1000多台设备使用gcm的方法。那样对吗?因为我不能尝试它,除非我有超过1000个用户,所以任何反馈都会很感激,最重要的是我是否正确地检查了错误?以及以正确的方式更新数据库Android GCM多搜索结果方法,android,google-cloud-messaging,Android,Google Cloud Messaging,这是我对1000多台设备使用gcm的方法。那样对吗?因为我不能尝试它,除非我有超过1000个用户,所以任何反馈都会很感激,最重要的是我是否正确地检查了错误?以及以正确的方式更新数据库 public class MessagingEndpoint { private static final Logger log = Logger.getLogger(MessagingEndpoint.class.getName()); /** * Api Keys can be obt
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");
private List<RegistrationRecord> records;
private List<String> getRegistrationId() {
records = ofy().load().type(RegistrationRecord.class).list();
List<String> records_ID = new ArrayList<String>();
for (int i = 0; i < records.size(); i++) {
records_ID.add(records.get(i).getRegId());
}
return records_ID;
}
private List<List<String>> regIdInThousands(List<String> list, final int L) {
List<List<String>> parts = new ArrayList<List<String>>();
final int N = list.size();
for (int i = 0; i < N; i += L) {
parts.add(new ArrayList<String>(list.subList(i, Math.min(N, i + L))));
}
return parts;
}
*
* @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<List<String>> regIdsParts = regIdInThousands(getRegistrationId(), 1000);
for (int i = 0; i < regIdsParts.size(); i++) {
MulticastResult multicastResult = sender.send(msg, regIdsParts.get(i), 5);
if (multicastResult.getCanonicalIds() != 0) {
List<Result> results = multicastResult.getResults();
for (int j = 0; j < results.size(); j++) {
if (results.get(j).getMessageId() != null) {
log.info("Message sent to " + regIdsParts.get(i).get(j));
String canonicalRegId = results.get(j).getCanonicalRegistrationId();
if (canonicalRegId != null) {
// if the regId changed, we have to update the datastore
log.info("Registration Id changed for " + regIdsParts.get(i).get(j) + " updating to " + canonicalRegId);
regIdsParts.get(i).set(j, canonicalRegId);
ofy().save().entity(records.get((i*1000)+j)).now();
} else {
String error = results.get(j).getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
log.warning("Registration Id " + regIdsParts.get(i).get(j) + " 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(records.get((i*1000)+j)).now();
} else {
log.warning("Error when sending message : " + error);
}
}
}
}
}
}
}
}
公共类MessaginEndpoint{
私有静态最终记录器log=Logger.getLogger(MessaginEndpoint.class.getName());
/**
*Api密钥可以从google云控制台获得
*/
私有静态最终字符串API_KEY=System.getProperty(“gcm.API.KEY”);
私人名单记录;
私有列表getRegistrationId(){
records=ofy().load().type(RegistrationRecord.class).list();
List records_ID=new ArrayList();
对于(int i=0;i1000){
message=message.substring(0,1000)+“[…]”;
}
发送方=新发送方(API_密钥);
Message msg=new Message.Builder().addData(“Message”,Message).build();
List regIdsParts=regidin数千(getRegistrationId(),1000);
对于(int i=0;i
您的代码看起来不错,我唯一能注意到的是它非常冗长和复杂。如果您在错误处理方面考虑周到,可以将此作为一个选项:
public void sendMessageToMultipleDevices(字符串键、字符串值、ArrayList设备){
Sender=newsender(myApiKey);
Message Message=new Message.Builder().addData(键,值).build();
试一试{
MulticastResult result=发送方.send(消息,设备,5);
MTLog.info(标记“result”+result.toString());
for(int i=0;i
当我阅读您的回复时,我在尝试发送gcm消息时收到一个503空指针。我想一些程序员可以在它发生之前看到它(哈哈:)。我将返工,并更新它,以获得您的饲料!谢谢:)
Sender sender = new Sender(myApiKey);
Message message = new Message.Builder().addData(key, value).build();
try {
MulticastResult result = sender.send(message, devices, 5);
MTLog.info(TAG, "result " + result.toString());
for (int i = 0; i < result.getTotal(); i++) {
Result r = result.getResults().get(i);
if (r.getMessageId() != null) {
String canonicalRegId = r.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// devices.get(i) has more than on registration ID: update database
}
} else {
String error = r.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from devices.get(i) - unregister database
}
}
}
} catch (IOException ex) {
MTLog.err(TAG, "sending message failed", ex);
}
}