Search in sources :

Example 1 with PushedNotifications

use of javapns.notification.PushedNotifications in project convertigo by convertigo.

the class PushNotificationStep method PushToAPNS.

// 
// seems like total size of Payload cannot exceed 256 bytes.
// 
protected void PushToAPNS(Context javascriptContext, Scriptable scope, List<Parameters> dictionary) throws EngineException, Exception {
    Engine.logBeans.debug("Push notification, Notifying IOS devices");
    evaluate(javascriptContext, scope, this.clientCertificate, "clientCertificate", false);
    sClientCertificate = evaluated instanceof Undefined ? "" : evaluated.toString();
    sClientCertificate = getAbsoluteFilePath(sClientCertificate);
    evaluate(javascriptContext, scope, this.certificatePassword, "certificatePassword", false);
    sCertificatePassword = evaluated instanceof Undefined ? "" : evaluated.toString();
    evaluate(javascriptContext, scope, this.useProductionAPNS, "ProductionCertificate", false);
    String sUseProductionCertificate = evaluated instanceof Undefined ? "" : evaluated.toString();
    boolean bUseProductionCertificate = "true".equalsIgnoreCase(sUseProductionCertificate);
    // get Token List
    StepSource tokens = getTokenSource();
    NodeList list;
    list = tokens.inError() ? null : tokens.getContextOutputNodes();
    if (list != null) {
        ArrayList<String> devicesList = new ArrayList<String>();
        for (int i = 0; i < list.getLength(); i++) {
            String token = getNodeValue(list.item(i));
            if (token.startsWith("apns:")) {
                devicesList.add(token.substring(5));
                Engine.logBeans.trace("Push notification, iOS device " + token.substring(5) + " will be notified");
            }
        }
        if (devicesList.isEmpty()) {
            Engine.logBeans.debug("Push notification, no iOS devices in the list");
            return;
        } else {
            Engine.logBeans.debug("Push notification, " + devicesList.size() + " iOS devices in the list. Sending to the " + (bUseProductionCertificate ? "production" : "development") + " APNS server");
        }
        try {
            // Submit the push to JavaPN library...
            PushedNotifications pn;
            if (dictionary.size() > 1) {
                /* Build a blank payload to customize */
                PushNotificationPayload payload = PushNotificationPayload.complex();
                for (int i = 0; i < dictionary.size(); i++) {
                    // if plugin specified, check it and skip accordingly
                    if ((dictionary.get(i).plug.length() != 0) && !(dictionary.get(i).plug.equalsIgnoreCase("aps") || dictionary.get(i).plug.equalsIgnoreCase("all"))) {
                        continue;
                    }
                    String value = dictionary.get(i).value;
                    if (dictionary.get(i).name.equalsIgnoreCase("alert")) {
                        payload.addAlert(value);
                    } else if (dictionary.get(i).name.equalsIgnoreCase("badge")) {
                        payload.addBadge(Integer.parseInt(value, 10));
                    } else if (dictionary.get(i).name.equalsIgnoreCase("sound")) {
                        payload.addSound(value);
                    } else {
                        if (dictionary.get(i).type.equalsIgnoreCase("int")) {
                            payload.addCustomDictionary(dictionary.get(i).name, Integer.parseInt(value, 10));
                        } else {
                            payload.addCustomDictionary(dictionary.get(i).name, value);
                        }
                    }
                }
                pn = Push.payload(payload, sClientCertificate, sCertificatePassword, bUseProductionCertificate, devicesList);
            } else {
                if (apnsNotificationType == ApnsNotificationType.Message) {
                    pn = Push.alert(dictionary.get(0).value, sClientCertificate, sCertificatePassword, bUseProductionCertificate, devicesList);
                } else if (apnsNotificationType == ApnsNotificationType.Badge) {
                    // mod jmc 07/10/2015
                    pn = Push.badge(Integer.parseInt(dictionary.get(0).value, 10), sClientCertificate, sCertificatePassword, bUseProductionCertificate, devicesList);
                } else {
                    pn = Push.sound(dictionary.get(0).value, sClientCertificate, sCertificatePassword, bUseProductionCertificate, devicesList);
                }
            }
            // Analyze the responses..
            for (PushedNotification notification : pn) {
                if (!notification.isSuccessful()) {
                    String invalidToken = notification.getDevice().getToken();
                    /* Find out more about what the problem was */
                    Exception theProblem = notification.getException();
                    /* If the problem was an error-response packet returned by Apple, get it */
                    ResponsePacket theErrorResponse = notification.getResponse();
                    if (theErrorResponse != null)
                        saveErrorForOutput("aps", invalidToken, "" + theErrorResponse.getIdentifier(), "", theErrorResponse.getMessage());
                    else
                        saveErrorForOutput("aps", invalidToken, "", "", theProblem.getMessage());
                }
            }
        } catch (KeystoreException e) {
            errorMessage = e.toString();
            Engine.logBeans.error("Push notification, IOS keystore exception : " + errorMessage);
        } catch (CommunicationException e) {
            /* A critical communication error occurred while trying to contact Apple servers */
            errorMessage = e.toString();
            Engine.logBeans.error("Push notification, IOS communication exception : " + errorMessage);
        } catch (Exception e) {
            errorMessage = "Push notification, IOS device exception: " + e.toString();
            Engine.logBeans.error(errorMessage);
        }
    }
}
Also used : Undefined(org.mozilla.javascript.Undefined) CommunicationException(javapns.communication.exceptions.CommunicationException) PushNotificationPayload(javapns.notification.PushNotificationPayload) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) PushedNotifications(javapns.notification.PushedNotifications) KeystoreException(javapns.communication.exceptions.KeystoreException) CommunicationException(javapns.communication.exceptions.CommunicationException) IOException(java.io.IOException) KeystoreException(javapns.communication.exceptions.KeystoreException) JSONException(org.codehaus.jettison.json.JSONException) EngineException(com.twinsoft.convertigo.engine.EngineException) StepSource(com.twinsoft.convertigo.beans.core.StepSource) PushedNotification(javapns.notification.PushedNotification) ResponsePacket(javapns.notification.ResponsePacket)

Aggregations

StepSource (com.twinsoft.convertigo.beans.core.StepSource)1 EngineException (com.twinsoft.convertigo.engine.EngineException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CommunicationException (javapns.communication.exceptions.CommunicationException)1 KeystoreException (javapns.communication.exceptions.KeystoreException)1 PushNotificationPayload (javapns.notification.PushNotificationPayload)1 PushedNotification (javapns.notification.PushedNotification)1 PushedNotifications (javapns.notification.PushedNotifications)1 ResponsePacket (javapns.notification.ResponsePacket)1 JSONException (org.codehaus.jettison.json.JSONException)1 Undefined (org.mozilla.javascript.Undefined)1 NodeList (org.w3c.dom.NodeList)1