4

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

  • Its auto managed by ios os, during application kill or not running state. So please correct your unicode format by followed apple Inc. – balkaran singh Aug 17 '16 at 10:56
  • Try putting a space between the unicode strings, also check your codes here: https://arashnorouzi.wordpress.com/2011/08/31/adding-graphics-and-emoji-characters-to-apple-push-notifications/ – Woodstock Aug 17 '16 at 10:56
  • Thanks Woodstock but user can put emoji without any space in our app during chat. Where android works fine and perfect. –  Aug 17 '16 at 11:06
  • @SoumyaDas I wonder if the issue lies with the unicode strings. If you use the codes at the link I provided, do they work then? – Woodstock Aug 17 '16 at 11:10
  • Yes sir, I know this format works, I already visit this type of site many time. The case is where and why i decode it at my web-service code side. please take a look on my java code. Thnaks@Woodstock –  Aug 17 '16 at 11:15
  • Have you tried removing `StringEscapeUtils.unescapeJava` entirely? – VGR Aug 17 '16 at 14:00
  • Yes, It solve my emoji issue on chat history so i put this on push notification case. It's show above on client side Iphone. After remove this 'StringEscapeUtils.unescapeJava' it shows on client side Iphone pn ?????.. @VGR –  Aug 19 '16 at 04:48
  • Is the client application using a font other than the default font? – VGR Aug 19 '16 at 14:50
  • Thanks for trying to helping me @VGR –  Aug 22 '16 at 11:14

2 Answers2

1

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

  • Calling alertMsg.getBytes followed by `new String`, both with the same charset, is the same as doing *nothing.* It is a round-trip identity operation. It does not change or fix your String in any way. You will get exactly the same result with `text = alertMsg;`. – VGR Aug 22 '16 at 13:18
  • Sir, I try this process but it sending push notification emoji ???? @VGR –  Aug 23 '16 at 09:37
0

you shouldn't need to mess about with html decoding. As you say the code point for smiling face is \u263A. In PHP you can represent that in a UTF8-encoded string as "\xE2\x98\xBA"

Lightning bolt (actually 'high voltage sign') is \u26A1 or "\xE2\x9A\xA1" in UTF-8.

Both these characters are present in some non-emoji fonts as regular Unicode symbols. You can see with:

<?php
header('Content-type: text/html; charset=utf-8');
echo "\xE2\x9A\xA1";
echo "\xE2\x98\xBA";

or other things...

for the googlers. json_encode() adds double \

$message = "\ue04a";
$body['aps'] = array(
                   'alert' => $message,
                   'sound' => 'default',
                   'type'  => $type,
                   'param' => $param
               );

$payload = json_encode($body);
$payload = str_replace("\", "\\", $payload);

Please check with this two way... i think this is helpfull for you.

Raju
  • 759
  • 3
  • 18
  • Respected iPhoneDev, my issue on java web-service side. Its works if i pass static '"String alertMsz="\ue04a hi"; ' –  Aug 19 '16 at 04:58