google 云信使中的参数不能为空异常

Argument cannot be null exception in google cloud messenger

我们正在使用 google 云信使推送通知。前几天还好好的,突然就开​​始报错了。

以下是堆栈跟踪

java.lang.IllegalArgumentException: argument cannot be null at com.google.android.gcm.server.Sender.nonNull(Sender.java:553) at com.google.android.gcm.server.Sender.getString(Sender.java:534) at com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:365) at com.google.android.gcm.server.Sender.send(Sender.java:261) at org.cdac.gist.restws.gcm.GCMSender.sendGCM(GCMSender.java:48) at org.cdac.gist.restws.handlers.GCMAndroidPushHandler.gcmSender(GCMAndroidPushHandler.java:156) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.invoke(ResourceMethodInvocationHandlerFactory.java:81) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.run(AbstractJavaResourceMethodDispatcher.java:144) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161) at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99) at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102) at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:326) at org.glassfish.jersey.internal.Errors.call(Errors.java:271) at org.glassfish.jersey.internal.Errors.call(Errors.java:267) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297) at org.glassfish.jersey.internal.Errors.process(Errors.java:267) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305) at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154) at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471) at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.cdac.gist.restws.security.ClickjackFilter.doFilter(ClickjackFilter.java:64) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)

以下是我的源代码

public final static int sendGCM(final String title, final List<String> registrationID)  throws IOException{         
            int status = 0;
            Sender sender = new Sender(apiKey);

            Message message = new Message.Builder()
                // .collapseKey("1")
                .timeToLive(86400)//sets the time of 1 day for GCM to push notifications when android device is off, offline or unavailable
                .delayWhileIdle(true)
                .addData("message", title)              
                .build();                        

            MulticastResult  result = sender.send(message, registrationID, 1);
            System.out.println("Result "+result);
                if(result == null){
                    throw new IOException("Could not send message");                    
                }else{
                    status = 1;
                }

                return status;  
    } 

问题是由于 GCM 支持最多 1,000 个收件人的单封邮件

所以我把我注册的ArrayList分成了1000等份,然后我把消息发给了GCM。现在我可以发送推送通知了。

以下是我更新后的代码

public final static int sendGCM(final String title, final List<String> registrationID)  throws IOException{         
        int status = 0;
        MulticastResult  result =null;


        for (int start = 0; start < registrationID.size(); start += 1000) {
            int end = Math.min(start + 1000, registrationID.size());
            List<String> sublist = registrationID.subList(start, end);
            System.out.println(sublist);
            Sender sender = new Sender(apiKey);

            Message message = new Message.Builder()
                // .collapseKey("1")
                .timeToLive(86400)//sets the time of 1 day for GCM to push notifications when android device is off, offline or unavailable
                .delayWhileIdle(true)
                .addData("message", title)              
                .build();                        

            result = sender.send(message, sublist, 1);
            System.out.println("Result "+result);
        }
         if(result == null){
                throw new IOException("Could not send message");                    
            }else{
                status = 1;
            }
            return status;  
        }