benratti / javapns

Automatically exported from code.google.com/p/javapns
0 stars 0 forks source link

Loosing messages using Queue #118

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
    DataInputStream in = new DataInputStream(req.getInputStream());
        List<PushNotificationNW> pushes = new ArrayList<PushNotificationNW>();
        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        reader.beginArray();
        while (reader.hasNext()) {
            pushes.add((PushNotificationNW) gson.fromJson(reader, PushNotificationNW.class));
        }
        reader.endArray();
        reader.close();

        if (pushes.size() == 0) {
            LOG.error("Push notification was null");
            return;
        }

        if (queue == null) { // lazy init queue

            try {
                queue = Push.queue(getServletContext().getResourceAsStream("/WEB-INF/keystore/token.p12"), "pass", false, 10);
            } catch (KeystoreException e) {
                e.printStackTrace();
                LOG.error("Keystore exception", e);
                InvalidPushResponse pushresp = new InvalidPushResponse(InvalidPushResponse.FATAL_KEYSTORE_ERROR, e.getMessage());
                List<InvalidPushResponse> responses = new ArrayList<InvalidPushResponse>();
                responses.add(pushresp);
                pushResponse(resp, responses);
                return;
            }

        }

        if (prodQueue == null) {

            try {
                prodQueue = Push.queue(getServletContext().getResourceAsStream("/WEB-INF/keystore/token.p12"), "passwd", true, 10);
            } catch (KeystoreException e) {
                e.printStackTrace();
                LOG.error("Keystore exception", e);
                InvalidPushResponse pushresp = new InvalidPushResponse(InvalidPushResponse.FATAL_KEYSTORE_ERROR, e.getMessage());
                List<InvalidPushResponse> responses = new ArrayList<InvalidPushResponse>();
                responses.add(pushresp);
                pushResponse(resp, responses);
                return;
            }

        }

        // attempt to send
        List<InvalidPushResponse> responses = new ArrayList<InvalidPushResponse>();
        List<PushedNotification> notifications = new ArrayList<PushedNotification>();
        String curToken = "";
        try {

            for (PushNotificationNW push : pushes) {
                if (!push.verifymd5()) {
                    LOG.error("md5 verifification exception:" + push.md5);
                    System.out.println("MD5 from server: " + push.md5);
                    System.out.println("MD5 from LOCAL: " + push.calcMD5());

                    continue;
                }
                System.out.println("push.production " + push.production + " TOKEN " + push.getPermissionToken() + "MESSAGE " + push.getAlert());
                curToken = push.getPermissionToken();
                try{
                    if (push.production)
                        prodQueue.add(push.getPayLoad(), push.getPermissionToken());
                    else
                        queue.add(push.getPayLoad(), push.getPermissionToken());
                }catch (JSONException e) {
                    ;//whatever... continue
                    LOG.error(e);
                    System.out.print(e.getMessage());
                }

            }

        } catch (InvalidDeviceTokenFormatException e) {
            System.out.print("Invalid token");
            LOG.error("Invalid token");
            responses.add(new InvalidPushResponse(InvalidPushResponse.INVALID_TOKEN, curToken));
        } 
        prodQueue.start();
        queue.start();

        // error handling
        notifications.addAll(queue.getPushedNotifications());
        queue.clearPushedNotifications();
        notifications.addAll(prodQueue.getPushedNotifications());
        prodQueue.clearPushedNotifications();

        for (PushedNotification notification : notifications) {
            if (notification.isSuccessful()) {
                /* Apple accepted the notification and should deliver it */
                System.out.println("Push notification sent successfully to: " + notification.getDevice().getToken());
                /* Still need to query the Feedback Service regularly */
            } else {

                String invalidToken = notification.getDevice().getToken();
                /* Add code here to remove invalidToken from your database */
                /* Find out more about what the problem was */
                Exception theProblem = notification.getException();
                theProblem.printStackTrace();
                responses.add(new InvalidPushResponse(InvalidPushResponse.INVALID_TOKEN, invalidToken));
                LOG.error("Unsuccessfull notification " + invalidToken);
                /*
                 * If the problem was an error-response packet returned by
                 * Apple, get it
                 */
                ResponsePacket theErrorResponse = notification.getResponse();
                if (theErrorResponse != null) {
                    System.out.println(theErrorResponse.getMessage());
                    LOG.error(theErrorResponse.getMessage());

                }
            }
        }
        pushResponse(resp, responses);

Original issue reported on code.google.com by oli...@crowdplayce.com on 26 Apr 2012 at 8:01

GoogleCodeExporter commented 9 years ago
sorry I thought I could see a preview. The following code is a servlet and it 
simply stops sending messages. I have no idea why

Original comment by oli...@crowdplayce.com on 26 Apr 2012 at 8:03

GoogleCodeExporter commented 9 years ago
Got this from the log, but nothing? IS this Apple?

2012-04-26 13:07:31,481 [JavaPNS grouped notification thread in QUEUE mode] 
DEBUG javapns.notification.PushNotificationManager- Building Raw message from 
deviceToken and payload
2012-04-26 13:07:31,481 [JavaPNS grouped notification thread in QUEUE mode] 
DEBUG javapns.notification.PushNotificationManager- Built raw message ID 
33554433 of total length 122
2012-04-26 13:07:31,481 [JavaPNS grouped notification thread in QUEUE mode] 
DEBUG javapns.notification.PushNotificationManager- Attempting to send 
notification: {"aps":{"sound":"Gun.wav","alert":"Hello World from 
Middy","badge":2},"pt":0}
2012-04-26 13:07:31,481 [JavaPNS grouped notification thread in QUEUE mode] 
DEBUG javapns.notification.PushNotificationManager-   to device: 
6658077df0c0bd42d34a81b171b73f6c7e7b36e4869d9a63b5d2d8b80591f380
2012-04-26 13:07:31,487 [JavaPNS grouped notification thread in QUEUE mode] 
INFO  javapns.notification.PushNotificationManager- Attempt failed (Remote host 
closed connection during handshake)... trying again
2012-04-26 13:07:31,487 [JavaPNS grouped notification thread in QUEUE mode] 
DEBUG javapns.communication.ConnectionToAppleServer- Creating SSLSocket to 
gateway.sandbox.push.apple.com:2195
2012-04-26 13:07:31,583 [JavaPNS grouped notification thread in QUEUE mode] 
DEBUG javapns.notification.PushNotificationManager- Attempting to send 
notification: {"aps":{"sound":"Gun.wav","alert":"Hello World from 
Middy","badge":2},"pt":0}
2012-04-26 13:07:31,583 [JavaPNS grouped notification thread in QUEUE mode] 
DEBUG javapns.notification.PushNotificationManager-   to device: 
6658077df0c0bd42d34a81b171b73f6c7e7b36e4869d9a63b5d2d8b80591f380
2012-04-26 13:07:31,866 [JavaPNS grouped notification thread in QUEUE mode] 
DEBUG javapns.notification.PushNotificationManager- Flushing
2012-04-26 13:07:31,866 [JavaPNS grouped notification thread in QUEUE mode] 
DEBUG javapns.notification.PushNotificationManager- At this point, the entire 
122-bytes message has been streamed out successfully through the SSL connection
2012-04-26 13:07:31,866 [JavaPNS grouped notification thread in QUEUE mode] 
DEBUG javapns.notification.PushNotificationManager- Notification sent on second 
attempt

Original comment by oli...@crowdplayce.com on 26 Apr 2012 at 1:12

GoogleCodeExporter commented 9 years ago
Closing as duplicate of issue #115.  Please provide requested information 
(stack trace dump) in the other issue report.

Original comment by sype...@gmail.com on 26 Apr 2012 at 9:49