【问题标题】:Execute push notification after sending message发送消息后执行推送通知
【发布时间】:2018-06-27 07:52:53
【问题描述】:

我正在使用sails.socket 进行消息发送。我的要求是,我必须在发送消息成功后发送推送通知。怎么可能。请看我写的代码。

sendChatMessage(chatMessage, function() {
    // calling push notification when a chat message send
    var serverKey       =   req.options.settingsKeyValue.PUSH_SERVER_KEY;
    var typeData        =   { type:1, data:{type:1} };
    var pushData        =   { title:'New Message', body: data };
    pusherService.pushFcm(serverKey,typeData,pushData,toId, function(err, result) {
        if(err) {
            return res.json(200, {status:1, status_type: 'Success', message: 'Error in  sending the message'});
        }else{
            return res.json(200, {status:1, status_type: 'Success', message: 'You have successfully send the message'});
        }
    });
});

function sendChatMessage(){    
    var socketRoom      =   "userRoom_"+toId;
    var roomsSubcribers =   sails.sockets.subscribers(socketRoom);
    console.log("roomsSubcribers");
    console.log(roomsSubcribers);
    var data            =   {
        text: message,
        from_id: fromId,
        from_name: userResult[0].firstName+' '+userResult[0].lastName, from_img : userResult[0].profilePhoto,
    };
    sails.sockets.broadcast(socketRoom,{
        type : "chat",
        message : data,

    });
    callback(chatMessage);
}

【问题讨论】:

    标签: node.js sockets websocket sails.js chat


    【解决方案1】:

    这些代码块对我来说看起来不错...您只需将它们放置在应用程序中的正确位置即可。你试过吗?

    您肯定需要在 sendChatMessage 函数定义和回调函数中命名一些参数。

    也许你需要这样的东西:

    // in some controller
    
    // notice the named arguments 'options' and 'callback'
    var sendChatMessage = function(options, callback){    
        var socketRoom      =   "userRoom_"+options.toId;
        var roomsSubcribers =   sails.sockets.subscribers(socketRoom);
        console.log("roomsSubcribers");
        console.log(roomsSubcribers);
        var data            =   {
            text: options.message,
            from_id: options.fromId,
            from_name: options.fromName, 
            from_img : options.fromImg,
        };
        sails.sockets.broadcast(socketRoom,{
            type : "chat",
            message : data,
        });
        callback(data);
    };
    
    module.exports = {
    
        someMethod: function(req, res) {
            // do some work, including defining / getting all options
            var options = {
                toId: 123,
                fromId: 456,
                message: 'test message',
                fromName: 'test user',
                fromImg: 'some/image.jpg' // don't know if you need a source here or what
            };
    
            // invoke your function - notice the named argument in the callback
            sendChatMessage(options, function(data) {
                // calling push notification when a chat message send
                var serverKey       =   req.options.settingsKeyValue.PUSH_SERVER_KEY;
                var typeData        =   { type:1, data:{type:1} };
                var pushData        =   { title:'New Message', body: data };
                pusherService.pushFcm(serverKey,typeData,pushData,toId, function(err, result) {
                    if(err) {
                        return res.json(200, {status:1, status_type: 'Success', message: 'Error in  sending the message'});
                    }else{
                        return res.json(200, {status:1, status_type: 'Success', message: 'You have successfully send the message'});
                    }
                });
            });
        },
    
    };
    

    我不知道这是否是使用所有插件等的正确方法,但将这些部分放在一起似乎很有意义。

    最后一点,当您定义自己的回调时,最好将第一个参数设为错误对象,并在回调正文中检查接收到的错误。成功后,您可以返回 null 错误,例如 callback(null, data);

    希望这有帮助。

    【讨论】:

    • 谢谢。让我试试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2017-02-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多