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

时间:2015-10-07 01:37:41

标签: json node.js mongoose apple-push-notifications

我正在使用节点js中的node-apn模块测试推送通知。一切正常,但单引号等特殊字符无法正常转义。带有单引号的推送通知(即我们将在实际通知中成为我们的对方)。我试过regex,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);                                                                    
                    }
                    })                                                                                                                                                                                    
        }                                                                                                                                                               
    }); 
}); 

1 个答案:

答案 0 :(得分:0)

我通过使用replace函数转义message.message值中的json formmatted单引号(//')解决了这个问题:

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);