APNS - 节点 APN - 有效负载 - 单引号

APNS - Node APN - payload - single quote

我正在使用 node js 中的 node-apn 模块测试推送通知。除了单引号等特殊字符无法正确转义外,一切正常。带有单引号的推送通知(即我们将在实际通知中变为 we\'ll)。我尝试了正则表达式,mongoose.toObject,message.message.toString() 无济于事。

Message.findOne(query).lean().exec(function (err, doc){

     if (!doc || doc == null) {                                              
                    message.save(function(e) {
                        console.log("message saved")
                        if (e) {
                            console.log("there is an error")
                            console.log(e)
                        } else {

                            console.log(message.device_token)

                            var mesg = message.toObject();
                            var msg = JSON.stringify(mesg);
                            var payload = {
                                "contact" : message.contact,
                                "did" : message.did,
                                "id" : message.message_id,
                                "date" : message.date,
                                "message" : message.message
                            }     

                            var clean = message.message.toString().replace(/[-[\]{}()*+?.,\^$|#\s]/g, "\$&");

                            var note = new apns.Notification();
                            var myDevice = new apns.Device(message.device_token);
                        note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
                        note.badge = 3;                                                                 
                        note.alert = message.message;
                        note.payload = payload;
                        apnsConnection.pushNotification(note, myDevice);                                                                    
                    }
                    })                                                                                                                                                                                    
        }                                                                                                                                                               
    }); 
}); 

我通过使用替换函数转义 message.message 值中的 json 格式化单引号(它是 //')解决了这个问题:

var messageStringCleaned = message.message.toString().replace(/\/g,"");                                                                                                                                                                            

    var payload = {
        "contact" : message.contact,
        "did" : message.did,
        "id" : message.message_id,
        "date" : message.date,
        "message" : messageStringCleaned
    }     

var note = new apns.Notification();
var myDevice = new apns.Device(message.device_token);
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = 3;                                                                 
note.alert = messageStringCleaned;
note.payload = payload;
apnsConnection.pushNotification(note, myDevice);