GCM MultiCastResult 方法

GCM MultiCastResult Approach

这是我对 1000 多台设备使用 gcm 的方法。这样对吗?因为除非我有超过 1000 个用户,否则我无法尝试,所以任何反馈都将不胜感激,最重要的是我是否正确检查了错误?并以正确的方式更新数据库?

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);
                            }
                        }
                    }
                }
            }
        }
    }
}

你的代码看起来不错,我唯一能注意到的是它相当冗长和复杂。如果您考虑到错误处理,您可以看看这个作为一个选项:

public void sendMessageToMultipleDevices(字符串键,字符串值,ArrayList 设备){

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);
    }
}