首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >APN -节点APN-有效载荷-单引号

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

Stack Overflow用户
提问于 2015-10-07 01:37:40
回答 1查看 709关注 0票数 0

我正在使用节点js中的node模块测试推送通知。除了单引号这样的特殊字符不能正确转义外,所有这些字符都可以正常工作。带有单引号的推送通知(即,我们将成为实际通知中的\‘),我尝试了regex、mongoose.toObject、message.message.toString(),但没有结果。

代码语言:javascript
复制
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);                                                                    
                    }
                    })                                                                                                                                                                                    
        }                                                                                                                                                               
    }); 
}); 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-12 22:44:21

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

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32982400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档