用于APN推送通知发送的Iphone表情符号Unicode解码问题

时间:2016-08-17 10:49:47

标签: java ios unicode apple-push-notifications javapns

我的网络服务发布推送通知到APN和APN发送到目标IOS设备。 当apns在警报正文推送通知中包含Unicode表情符号时,Iphone操作系统无法解码我的Unicode表情符号' \ uD83D \ uDE0A '应用程序已杀死。

推送通知显示相同的' \ uD83D \ uDE0A ',顶部的横幅通知栏上没有显示表情符号。

通过GCM调度推送通知,Android应用程序正常运行但是IOS不支持。 Iphone-Ios仅支持此格式' \ ue415 '

这里来自ActiveMQ订阅聊天有效负载的代码进入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();
            }
        }

我在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;
    }

}

发布到APNs代码

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 OS管理的Ios推送通知

我的Apache Catalina日志 Apache Catalina log 我是一名网络服务云开发人员,在过去一天只面对这个问题,只有Ios应用程序。所以,如果有些人知道或以前做过,请。请告诉我一些想法。 我的推送通知链接中的表情符号。 https://mixpanel.com/help/questions/articles/how-do-i-send-custom-parameters-like-emoji-in-my-push-notifications 感谢

2 个答案:

答案 0 :(得分:1)

最后,APNs通过此Unicode编码和解码过程在java Web服务代码上解决了(Ios应用程序)问题。 ( unescapeJava和escapeJava )来自lib commons-lang-2.6.jar 和类 org.apache.commons.lang.StringEscapeUtils

  

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

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 尊敬的先生和我是,如果在IOS推送通知上有任何其他Java APNs emoji Unicode的解决方案。

然后请给我一些提示。
谢谢

答案 1 :(得分:0)

你不应该搞乱html解码。正如你所说,笑脸的代码点是\ u263A。在PHP中,您可以将UTF8编码的字符串表示为“\ xE2 \ x98 \ xBA”

闪电螺栓(实际上是'高压符号')是UTF-8中的\ u26A1或“\ xE2 \ x9A \ xA1”。

这些字符都作为常规Unicode符号存在于某些非表情符号字体中。你可以看到:

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

或其他事情......

对于googlers。 json_encode()添加了双\

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

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

请用这两种方式检查......我认为这对你有帮助。