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

    • 0
    • 3
    • 1
    • 3
    • 1
    • 0
    • 0
    • 0
    • 1.64k
    Comment on it

    This blog will help you to send Push Notifications with Custom data 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 your pom.xml, if you are using maven:

    <dependency>
        <groupId>com.github.fernandospr</groupId>
        <artifactId>javapns-jdk16</artifactId>
        <version>2.2.1</version>
    </dependency>
    

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

    1. Send Push Notification with Custom Data to iphone device:

    Write the following metthod to send Pust Notification with Custom Data to iphone devices:

    public Boolean sendPushNotificationForiPhone(String message, List<String> 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 {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("firstName", "Aakash");
    
                /* Create a blank payload to customize */
                PushNotificationPayload payload = PushNotificationPayload.complex();
    
    
                /* Now add the custom values to payload */
                payload.addAlert(message);
                payload.addCustomDictionary("custom&#95;fields", jsonObject.toString());
    
    
                List<PushedNotification> notifications = Push.payload(payload, new File(fileName.toString()), "", true, device.getDeviceAppId());
                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(JSONException e)
            {
                logger.debug("JSONException" + e.getLocalizedMessage());
                e.printStackTrace();
            } 
            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 with Custom Data to an iPhone Device from Java Server side as this will help other people who read the post.

 1 Comment(s)

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: