kaylavanhaverbeck / javapns

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

Client already exists #65

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Client already exists
at javapns.back.DeviceFactory.addDevice(DeviceFactory.java:68)
at 
javapns.back.PushNotificationManager.addDevice(PushNotificationManager.java:132)
at 
com.shci.shms.materialmanagement.spring.controller.TCPushNotificationController.
Push(TCPushNotificationController.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at 
org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeName
dMethod(MultiActionController.java:445)
at 
org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequ
estInternal(MultiActionController.java:383)
at 
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractCon
troller.java:153)
at 
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(Simple
ControllerHandlerAdapter.java:48)
at 
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.j
ava:858)
at 
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.ja
va:792)
at 
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet
.java:476)
at 
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:44
1)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilt
erChain.java:290)
at 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.
java:206)
at com.shci.shms.common.filter.TCFilter.doFilter(TCFilter.java:76)
at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilt
erChain.java:235)
at 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.
java:206)
at 
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:9
6)
at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilt
erChain.java:235)
at 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.
java:206)
at 
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:2
30)
at 
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:1
75)
at 
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociatio
nValve.java:179)
at 
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at 
org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionVa
lve.java:157)
at 
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109
)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at 
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Pr
otocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)

Actual Code

private static Logger log = Logger.getLogger(TCPushNotificationController.class);   

private TCPushNotificationService tcPushNotificationService = null;

// APNs Server Host & port
private static final String HOST = "gateway.push.apple.com";
private static final int PORT = 2195;

// Badge
private static final int BADGE = 66;

// iPhone's UDID (64-char device token)
//private static String iPhoneId = "b92264f8f341db3fbb4255eb7a378bebc10f71c5fd9953d1c8129ee5d6ea817a";
private static String certificate = "c:/Certificates.p12";
private static String passwd = "12345";

public ModelAndView Push(HttpServletRequest request, HttpServletResponse response) throws Exception {

    response.setHeader("Cache-Control", "no-store");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Pragma", "no-cache");   
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");

    String iPhoneToken = (String) request.getParameter("iPhoneToken");  
    String status = (String) request.getParameter("status");        
    String iPhoneId = iPhoneToken;

    String msg = "Order Status : " + status;

    log.info( "Setting up Push notification" );

   try {
        // Setup up a simple message
        PayLoad aPayload = new PayLoad();
        //aPayload.addBadge( BADGE );

        //aPayload.addAlert("Franklin Alert Message #2");
        aPayload.addAlert(msg);            

        log.info( "Payload setup successfull." );

        log.info ( "~~~~~aPayload="+aPayload );

        // Get PushNotification Instance
        PushNotificationManager pushManager = PushNotificationManager.getInstance();

        // Link iPhone's UDID (64-char device token) to a stringName
        pushManager.addDevice("iPhone", iPhoneId);
        log.info( "iPhone UDID taken." );

        log.info( "Token: " + pushManager.getDevice( "iPhone" ).getToken() );

        // Get iPhone client
        Device client = pushManager.getDevice( "iPhone" );
        log.info( "Client setup successfull." );

        // Initialize connection
        pushManager.initializeConnection( HOST, PORT, certificate, passwd, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);

        log.info( "Connection initialized..." );

        //pushManager.setRetryAttempts(100);

        // Send message
        pushManager.sendNotification( client, aPayload );
        log.info( "Message sent!" );

        log.info( "# of attempts: " + pushManager.getRetryAttempts() );

        pushManager.stopConnection();
        //pushManager.removeDevice(iPhoneId);
        //PushNotificationManager.getInstance().removeDevice(iPhoneId);

        //pushManager = null;

        log.info( "done" );

    } catch (Exception e) {
        //e.printStackTrace();
        log.error("Exception Caught : ", e);
    }

    return null;
}

Os : Window XP
Jboss : 4.2.1 
Java JDK : 1.5.0

Original issue reported on code.google.com by franklin...@gmail.com on 4 Sep 2011 at 1:01

GoogleCodeExporter commented 8 years ago
Your code always adds devices with the same ID ("iPhone" in your case).  Thus 
if you call your Push method twice, you will indeed get a duplicate device 
exception (ie "Client already exists").

SOLUTION FOR JAVAPNS 1.x:  You should make sure that devices are added with 
unique IDs.  Using the device token as the ID is a good way of ensuring unique 
IDs:  pushManager.addDevice(iPhoneId, iPhoneId);    and then later:  Device 
client = pushManager.getDevice(iPhoneId);

SOLUTION FOR JAVAPNS 2.0:  In 2.0, there is no need to add and get devices when 
no persistence backend is required.  The simple "javapns.Push" class allows you 
to push notifications with a single line of code (see wiki).

Original comment by sype...@gmail.com on 6 Sep 2011 at 4:29