Search code examples
javaiospush-notificationpayload

Failed to send notifications to iOs devices


I'm implementing notifications from a .jar, notifications in work perfectly, but lately the notifications in are giving me a error which is as follows:

Delivery error: javapns.notification.exceptions.PayloadMaxSizeExceededException:
Total payload size exceeds allowed limit (payload is 259 bytes, limit is 256)

The code I'm using is as follows:

    //El metodo de multithread payload hace return unicamente cuando todos los hilos han finalizado de poner las notificaciones.
    //Como no queremos esperar a que termine ponemos el codigo en un hilo separado para que pueda continuar y no se quede colgado el WS.            
    Thread thread = new Thread(){
        public void run(){              
            try {           
                final List<String> idsDispositivos = new ArrayList<String>();                   

                final String keystore = XmlUtils.dirCertIOS + XmlUtils.nomCertificado;
                final String password = XmlUtils.password;
                final boolean production = XmlUtils.production;

                //Obtenemos los ids de los dispositivos
                for(DispositivoDto r : dispositivos)
                     idsDispositivos.add(r.getIdDispositivo());                  

                // Creamos el payload con los campos que necesitemos       
                PushNotificationPayload payload = PushNotificationPayload.complex();    

                /* Customize the payload */ 
                payload.addAlert(textoES);   
                payload.setContentAvailable(true);

                payload.addCustomDictionary("es", textoES);
                payload.addCustomDictionary("en", textoCA);
                payload.addCustomDictionary("ca", textoEN);
                payload.addCustomDictionary("tiponotificacion", tipoNotificacion);  

                List<PushedNotification> notifications = new ArrayList<PushedNotification>();

                if(idsDispositivos.size()<= 200){
                    /* Push your custom payload */    
                    notifications = Push.payload(payload, keystore, password, production, idsDispositivos);

                // Si hay mas de 200 dispositivos creamos diferentes hilos utilizando el multithread payload
                } else {
                    // Decidimos cuantos hilos vamos a crear y usar
                    int threads = 30;

                    if(dispositivos.size() > 200) {
                        threads = (int) Math.ceil(dispositivos.size()/200);
                    }else{
                        threads = 1;
                    }

                    // empezamos los hilos, esperamos a que terminen, y se coge la lista de notificaciones
                    notifications = Push.payload(payload, keystore, password, production, threads, idsDispositivos);
                }

                //Gestion de los resultados del envio de notificaciones 
                int dispEliminados = 0;
                int dispNotificados = 0;

                for (PushedNotification notification : notifications) {
                    if (notification.isSuccessful()) {
                        dispNotificados ++;
                        // Apple accepta la notificacion y debe enviarla
                    } else {
                        String invalidToken = notification.getDevice().getToken();

                        //obtenemos el indice del dispositivo en la lista de dispositivos
                        int index = idsDispositivos.indexOf(invalidToken);

                        //obtenemos la informacion del dispositivo a borrar
                        Integer usuario = dispositivos.get(index).getUsuario();
                        String idHardware = dispositivos.get(index).getIdHardwareDis();

                        //quitamos el dispositivo de nuestra BD
                        aBD.unregisterDispositivo(usuario, invalidToken,idHardware);
                        dispEliminados ++;                          

                        //Encontramos mas informacion sobre el problema ocurrido
    //                  Exception theProblem = notification.getException();
    //                  theProblem.printStackTrace();

                        //If the problem was an error-response packet returned by Apple, get it
                        ResponsePacket theErrorResponse = notification.getResponse();

                        if (theErrorResponse != null){
                            logNot.info("IOS: " +theErrorResponse.getMessage());
                        }
                    }
                }
            } catch (CommunicationException e) {
                logNot.error("IOS: Error en envio notificaciones - CommunicationException: ",e);
            } catch (KeystoreException e) {
                logNot.error("IOS: Error en envio notificaciones - KeystoreException: ",e);
            } catch (JSONException e) {
                logNot.error("IOS: Error en envio notificaciones - JSONException: ",e);
            } catch (Exception e) {
                logNot.error("IOS: Error en envio notificaciones",e);
            }
        }
    };

    thread.start();

Any idea?


Solution

  • I managed to remove the error:

    Delivery error: javapns.notification.exceptions.PayloadMaxSizeExceededException:
    Total payload size exceeds allowed limit (payload is 259 bytes, limit is 256)

    For this I had to modify the line:

    PushNotificationPayload payload = PushNotificationPayload.complex(); 
    

    By this other:

    PushNotificationPayload payload = PushNotificationBigPayload.complex();