javaiosunicodeapple-push-notificationsjavapns

Iphone emoji Unicode decode issue for push notification dispatch by APNs


My web service publish push notification to APNs and APNs send to destination IOS device. When apns contain Unicode emoji on alert body push notification and Iphone os can't decode my Unicode emoji '\uD83D\uDE0A' app already kill.

Push notification show same '\uD83D\uDE0A', No emoji shown on banner notification bar on top.

Android application works fine by GCM dispatches push notification But IOS not support. Iphone-Ios supports only this format '\ue415'

Here code that from ActiveMQ subscribe chat payload get into web-service

            public void onPublish(UTF8Buffer topic, Buffer msg, Runnable ack) {
            try {

                String body = msg.utf8().toString();
                if (logger.isInfoEnabled()) {
                    logger.info("MQTT connection.listener.onPublish(), msg Received ["
                            + body + "]");
                }
                if (body.contains("\"cmd\":\"chat\"")
                        && body.contains("\"is_sender_msg\":true")) { 
                    QueueMgr.addToChatQueue(body); //Changed true to false
                }
                else if(body.contains("\"cmd\":\"msg_seen\"")){
                    QueueMgr.addToChatReadSeenQueue(body);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                ack.run();
            }
        }

My code for create push notification on java

public static JSONObject constructePushJson(JSONObject jsonObject,String[] cloudkeyWithDevice) throws JSONException {
    if(cloudkeyWithDevice[0] != null){
        JSONObject pnAPIdata = new JSONObject();
            if(cloudkeyWithDevice[1].equals("a") || cloudkeyWithDevice[1].equals("d")){
                pnAPIdata.put(com.anyorg.constants.AppConstants.FLD_CMD, com.anyorg.constants.AppConstants.CMD_ANDROID_PUSH);
            }
            else{
                pnAPIdata.put(com.anyorg.constants.AppConstants.FLD_CMD, com.anyorg.constants.AppConstants.CMD_IOS_PUSH);
            }
            pnAPIdata.put(com.anyorg.constants.AppConstants.FLD_APP_TOKEN, com.anyorg.constants.AppConstants.DEFAULT_APP_TOKEN);
            pnAPIdata.put(com.anyorg.constants.AppConstants.FLD_DEVICE_TOKEN, cloudkeyWithDevice[0]);
            pnAPIdata.put(com.anyorg.constants.AppConstants.FLD_USER_ID, jsonObject.getInt(com.anyorg.constants.AppConstants.FLD_TO_USER_ID));
            pnAPIdata.put(com.anyorg.constants.AppConstants.FLD_DEVICE_ID, 0);
            String alertMsg=StringEscapeUtils.unescapeJava(jsonObject.getString(com.anyorg.constants.AppConstants.FLD_BODY));
            jsonObject.put(com.anyorg.constants.AppConstants.FLD_BODY,alertMsg);
            pnAPIdata.put(com.anyorg.constants.AppConstants.FLD_ALERT_MSG, "AryaConnect: "+alertMsg);//(jsonObject.isNull("body")) ? jsonObject.getString("from_user_name")+": Sent a file" : jsonObject.getString("from_user_name")+": "+jsonObject.getString("body")
            pnAPIdata.put(com.anyorg.constants.AppConstants.FLD_MSG, jsonObject);//jsonObject.getString(com.anyorg.constants.AppConstants.FLD_BODY)
            pnAPIdata.put(com.anyorg.constants.AppConstants.FLD_CALLBACK_URL, callbackUrl);
            pnAPIdata.put(com.anyorg.constants.AppConstants.MAC_ADDRESS_ID, jsonObject.getString("mobile_rec_id"));
            return pnAPIdata;
    }
    else{
        return null;
    }

}

Publish to APNs code

public class ANSNotificationDispatcher implements NotificationDispatcher {
protected static final Logger logger = Logger
        .getLogger(ANSNotificationDispatcher.class);

public static final String OS_NAME = AppConstants.OS_TYPE_IPHONE;

String keystore;
String password;
boolean production;

public ANSNotificationDispatcher() {
    try {
        keystore = AppConfig.getAPNKeystore();
        password = AppConfig.getAPNKeystorePassword();
        PushyAPNMgr.init(keystore, password, AppConfig.isAPNProdcution());
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

private void push(Payload payload, String token, String userId,
        String deviceId) throws ConfigurationException,
        DeviceUnregisteredException {

    // QueueManager.addToIOsQueue(payLoad, token, userId, ivUserDeviceId);
    long stime = System.currentTimeMillis();
    try {
        PushyAPNMgr.push(token, payload.toString());
        if (logger.isInfoEnabled())
            logger.info("push(): APN PN userId [" + userId
                    + "], device id [" + deviceId + "] payoad [" + payload
                    + "] Response time ["
                    + (System.currentTimeMillis() - stime) + "]ms");
    } catch (Exception e) {
        e.printStackTrace();
        throw new ConfigurationException();
    }
}

public static Payload createComplexPayload(JSONObject jsonObject) {

    PushNotificationPayload complexPayload = null;
    try {
        complexPayload = createPayload(jsonObject);
        String msg = Common.getStringAsNull(jsonObject,
                AppConstants.FLD_MSG);
        if (!Common.isEmpty(msg)) {
            complexPayload.addCustomDictionary(AppConstants.FLD_MSG, msg);
        }
        if (logger.isInfoEnabled()) {
            logger.info("createComplexPayloadV2(): payload ["
                    + complexPayload.getPayload().toString() + "]");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return complexPayload;
}

public void dispatch(JSONObject jsonObject, String jsonData)
        throws NotificationException, DeviceUnregisteredException,
        MultipleRegistartionIdException, ConfigurationException {

    String deviceToken = Common.getStringAsNull(jsonObject,
            AppConstants.FLD_DEVICE_TOKEN);
    if (Common.isEmpty(deviceToken)) {
        logger.error("dispatch(): device token is null, cmd [" + jsonData
                + "]");
        return;
    }
    Payload payload = createComplexPayload(jsonObject);

    String userId = Common.getStringAsNull(jsonObject,
            AppConstants.FLD_USER_ID);
    String deviceId = Common.getStringAsNull(jsonObject,
            AppConstants.FLD_DEVICE_ID);
    push(payload, deviceToken, userId, deviceId);
}

public static void handleInvalidTokeException(String token) {
}

public static void handleDeviceUnregisteredException(String token) {
}

}

Ios push notification managed by Ios OS

Ios push notification managed by Ios OS

My Apache Catalina log Apache Catalina log I am a web service cloud developer faceing this issue last one days for only Ios app. So please, if some body have knowledge or done before. please advise and refer me some idea. Emoji in my push notifications link. https://mixpanel.com/help/questions/articles/how-do-i-send-custom-parameters-like-emoji-in-my-push-notifications Thanks


Solution

  • Finally, APNs issue resolve in (Ios app) on java web service code by this Unicode encode and decode process. (unescapeJava and escapeJava) from lib commons-lang-2.6.jar and class org.apache.commons.lang.StringEscapeUtils

    emojiBytes = alertMsg.getBytes("UTF-8");
    text = new String(emojiBytes, "UTF-8");

    private static PushNotificationPayload createPayload(JSONObject jsonObject)
            throws JSONException {
    
        String alertMsg = Common.getStringUnicodeAsNull1(jsonObject,
                AppConstants.FLD_ALERT_MSG);
        byte[] emojiBytes=null;
        String text=null;
        try {
            emojiBytes = alertMsg.getBytes("UTF-8");
            text = new String(emojiBytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //String emojiAsString = new String(emojiBytes, Charset.forName("UTF-8"));
        //System.out.println("@@@@@alertMsg: "+text);
        Integer badgeCnt;
        if (jsonObject.has(AppConstants.FLD_BADGE_CNT)){
            badgeCnt = Common.getIntegerAsNull(jsonObject,
                    AppConstants.FLD_BADGE_CNT);
        }else{
            badgeCnt = AppConstants.VAL_ZERO;
        }
        PushNotificationPayload payload = createPayload(badgeCnt, text);
        return payload;
    }
    

    String alertMsg = Common.getStringUnicodeAsNull1(jsonObject, AppConstants.FLD_ALERT_MSG);

    public static String getStringUnicodeAsNull1(JSONObject jsonObject,
            String key) {
        try {
            if(jsonObject.isNull(key))
                return null;
            else
            return StringEscapeUtils.unescapeJava(jsonObject.getString(key));
        } catch (JSONException je) {
            return null;
        }
    }
    

    enter image description here Respected sir and ma'am, If there is any other solution of java APNs emoji Unicode on IOS Push Notification.

    Then please give me some hints.
    Thanks