JAVAPNS 2.2成功推送但未在设备上接收

时间:2012-07-05 10:20:11

标签: java apple-push-notifications

在这里输入代码我尝试使用JAVAPNS 2.2从Java推送

这是我的代码

import java.util.*;
import org.apache.log4j.BasicConfigurator;
import org.json.JSONException;
import javapns.*;
import javapns.communication.exceptions.CommunicationException;
import javapns.communication.exceptions.KeystoreException;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;
import javapns.notification.ResponsePacket;



public class Test {

/**
 * @param args
 */
public static void main(String[] args) {

    BasicConfigurator.configure();

    try {

        PushNotificationPayload payload = PushNotificationPayload.complex();

        payload.addAlert("Hello World");
        payload.addBadge(1);
        payload.addSound("default");
        payload.addCustomDictionary("id", "1");


        System.out.println(payload.toString());
        List<PushedNotification> NOTIFICATIONS = Push.payload(payload, "D:\\keystore1.p12", "123456", true, "AA67F2F18586D2C83398F9E5E5BE8BA1CD3FF80257CC74BACF2938CE144BA71D");

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

                    /* 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());
                    }
            }
      }


    } catch (CommunicationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeystoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

图书馆发送成功,但我从来没有推过我的设备。我尝试了许多我的设备令牌,甚至尝试重新生成证书.12但仍然没用。

日志

0 [main] DEBUG javapns.notification.Payload  - Adding alert [Hello World]

1 [main] DEBUG javapns.notification.Payload  - Adding badge [1]

1 [main] DEBUG javapns.notification.Payload  - Adding sound [default]

1 [main] DEBUG javapns.notification.Payload  - Adding custom Dictionary [id] = [1]

{"id":"1","aps":{"sound":"default","alert":"Hello World","badge":1}}

292 [main] DEBUG javapns.communication.ConnectionToAppleServer  - Creating SSLSocketFactory

341 [main] DEBUG javapns.communication.ConnectionToAppleServer  - Creating SSLSocket to gateway.push.apple.com:2195

972 [main] DEBUG javapns.notification.PushNotificationManager  - Initialized Connection to 
Host: [gateway.push.apple.com] Port: [2195]: 1923e91b[SSL_NULL_WITH_NULL_NULL: Socket[addr=gateway.push.apple.com/17.149.35.173,port=2195,localport=65225]]

974 [main] DEBUG javapns.notification.PushNotificationManager  - Building Raw message from deviceToken and payload

974 [main] DEBUG javapns.notification.PushNotificationManager  - Built raw message ID 1 of total length 113

974 [main] DEBUG javapns.notification.PushNotificationManager  - Attempting to send notification: {"id":"1","aps":{"sound":"default","alert":"Hello World","badge":1}}

974 [main] DEBUG javapns.notification.PushNotificationManager  -   to device: AA67F2F18586D2C83398F9E5E5BE8BA1CD3FF80257CC74BACF2938CE144BA71D

1743 [main] DEBUG javapns.notification.PushNotificationManager  - Flushing

1743 [main] DEBUG javapns.notification.PushNotificationManager  - At this point, the entire 113-bytes message has been streamed out successfully through the SSL connection

1743 [main] DEBUG javapns.notification.PushNotificationManager  - Notification sent on first attempt

1743 [main] DEBUG javapns.notification.PushNotificationManager  - Reading responses

1744 [main] DEBUG javapns.notification.PushNotificationManager  - Closing connection
PUSH NOTIFICATION SENT SUCCESSFULLY TO: AA67F2F18586D2C83398F9E5E5BE8BA1CD3FF80257CC74BACF2938CE144BA71D

任何人都有任何想法?

1 个答案:

答案 0 :(得分:0)

此行应该

List<PushedNotification> NOTIFICATIONS = Push.payload(payload, "D:\\keystore1.p12", "123456", true, "AA67F2F18586D2C83398F9E5E5BE8BA1CD3FF80257CC74BACF2938CE144BA71D");

应该改为

List<PushedNotification> NOTIFICATIONS = Push.payload(payload, "D:\\keystore1.p12", "123456", false, "AA67F2F18586D2C83398F9E5E5BE8BA1CD3FF80257CC74BACF2938CE144BA71D");