Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json APNS-节点APN-有效负载-单报价_Json_Node.js_Mongoose_Apple Push Notifications - Fatal编程技术网

Json APNS-节点APN-有效负载-单报价

Json APNS-节点APN-有效负载-单报价,json,node.js,mongoose,apple-push-notifications,Json,Node.js,Mongoose,Apple Push Notifications,我正在使用NodeJS中的NodeAPN模块测试推送通知。除了特殊字符(如单引号)不能正确转义外,所有字符都可以正常工作。带有单个报价的推送通知(即,我们将成为实际通知中的we'll)。我尝试了regex、mongoose.toObject、message.message.toString()但没有成功 Message.findOne(query).lean().exec(function (err, doc){ if (!doc || doc == null) {

我正在使用NodeJS中的NodeAPN模块测试推送通知。除了特殊字符(如单引号)不能正确转义外,所有字符都可以正常工作。带有单个报价的推送通知(即,我们将成为实际通知中的we'll)。我尝试了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);                                                                    
                    }
                    })                                                                                                                                                                                    
        }                                                                                                                                                               
    }); 
}); 

我通过使用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);