Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Send Push Notification to iPhone device from Java Server Side

    • 0
    • 4
    • 1
    • 2
    • 28
    • 0
    • 0
    • 0
    • 4.46k
    Comment on it

    This blog will help you to send Push Notifications to iPhone devices from server side where server side code is written in Java.

    Follow the below code in order to send Push Notification:

    1. Define the below dependency in you pom.xml, if you are using maven:
    2.            com.github.fernandospr
                 javapns-jdk16
                 2.2.1
                

      Otherwise, put the JavaPNS_2.2.jar into your lib folder of your project.

    3. Send Push Notification to iphone device:

      Write the following metthod to send Pust Notification to iphone devices:

    public Boolean sendPushNotificationForiPhone(String message, List devices) 
        {
            Boolean notificationFlag = false;
            //Here you will get resource path of you project
            URL resource = UserService.class.getResource("/");
            //Here you will define path to the folder where you hav saved p12 file for iphone 
            String  path = resource.getPath().replace("WEB-INF/classes/", "keystore/whami_push.p12");
            String fileName = path;
            try {
                List notifications = Push.alert(message, new File(fileName.toString()), "", false, devices);
                for (PushedNotification notification : notifications) {
                    if (notification.isSuccessful()) 
                    {
                        notificationFlag = true;
                    } 
                    else 
                    {
                        String invalidToken = notification.getDevice().getToken();
                        notificationFlag = false;
                        // Add code here to remove invalidToken from your
    
    
                        // Find out more about what the problem was 
                        Exception theProblem = notification.getException();
                        theProblem.printStackTrace();
    
                        ResponsePacket theErrorResponse = notification.getResponse();
    
                        if (theErrorResponse != null) 
                        {
                            logger.debug(theErrorResponse.getMessage());
                        }
                    }
                }
    
            } catch (KeystoreException e) {
                // A critical problem occurred while trying to use your keystore 
                logger.debug("KeystoreException" + e.getLocalizedMessage());
                notificationFlag = false;
                e.printStackTrace();
    
            } catch (CommunicationException e) {
    
                // A critical communication error occurred while trying to contact Apple servers
                notificationFlag = false;
                logger.debug("CommunicationException");
                e.printStackTrace();
            }
            return notificationFlag;
    
        }
    

    Please feel free to share the experiences you had while sending Push Notifications to an iphone Device from Java Server side as this will help other people who read the post.

 28 Comment(s)

  • H Ravindra, you can find import statement as below:

    import javapns.Push;
    import javapns.communication.exceptions.CommunicationException;
    import javapns.communication.exceptions.KeystoreException;
    import javapns.notification.PushedNotification;
    import javapns.notification.ResponsePacket;
    
  • I'm not able to provide you any straight answer....can you please tell me what are you getting in resource variable?or you are getting any error on that line? Please confirm this on first then I'll answer your further questions.

  • please give me a straight answer to my question,I have pasted p12 file in build/clasess folder of my project..so what i have to write in below lines 1)URL resource = iphone.class.getResource("/"); 2)String path = resource.getPath().replace("build/classes/", "Krishna.p12");.please leave a reply

  • just one straight question..i have copied p12 file and pasted it in the build/classes folder of my project..so what i have to write in these two lines 1)URL resource = iphone.class.getResource("/"); 2)String path = resource.getPath().replace("build/classes/", "/Krishna.p12");

  • /C:/Users/omsairam/iphone/build/classes/ log4j:WARN No appenders could be found for logger (javapns.notification.Payload). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. KeystoreExceptionInvalid keystore reference. Path does not refer to a valid file: C:\Users\omsairam\iphone\build\classesjavapns.communication.exceptions.InvalidKeystoreReferenceException: Invalid keystore reference. Path does not refer to a valid file: C:\Users\omsairam\iphone\build\classes at javapns.communication.KeystoreManager.validateKeystoreParameter(KeystoreManager.java:220) at javapns.communication.AppleServerBasicImpl.(AppleServerBasicImpl.java:31) at javapns.notification.AppleNotificationServerBasicImpl.(AppleNotificationServerBasicImpl.java:57) at javapns.notification.AppleNotificationServerBasicImpl.(AppleNotificationServerBasicImpl.java:42) at javapns.notification.AppleNotificationServerBasicImpl.(AppleNotificationServerBasicImpl.java:28) at javapns.Push.sendPayload(Push.java:170) at javapns.Push.alert(Push.java:47) at iphone.sendPushNotificationForiPhone(iphone.java:21) at iphone.main(iphone.java:58)

  • i have attached clearly all my code and also shown where i provided the p12 file in the project..so please let me know about the errors in the code..my problem is iam not understanding what to write in lines 16 and 17

  • ok then if i keep p12 file in my WEB-INF/classes/ folder is there need of below lines in my code?? 1)URL resource = iphone.class.getResource("/"); 2)String path = resource.getPath().replace("WEB-INF/classes/", "keystore/whami_push.p12");

  • public static void main(String args[]) { iphone o=new iphone(); o.sendPushNotificationForiPhone("Hello World",???); } this is the main function i have in my code..what should be written in the place of ???

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: