parse-platform - 解析后台作业可以向当前登录的所有用户发送消息吗?

标签 parse-platform

我是 Parse 的新手,我想知道是否有办法安排一个后台作业,该作业每 3 分钟启动一次,并向当时登录的所有用户发送一条消息(整数或其他内容)。我找不到任何帮助 here阅读指南。我希望有人能在这里帮助我。

最佳答案

我需要在几个使用 Parse.com 构建的应用程序中为所有登录用户推送信息。

Emilio 之前介绍的解决方案都不是,因为我们需要只为登录用户触发一些实时事件。

因此我们决定在 CloudCode 中使用 PubNub in Parse:http://www.pubnub.com/blog/realtime-collaboration-sync-parse-api-pubnub/

我们的策略是为所有用户打开一个可用的“ channel ”,如果用户处于事件状态(已登录),我们将向这个专用“ channel ”推送一些由应用程序触发的信息,并创建一些新的事件或号召性用语。

这是将信息发送到专用 channel 的示例代码:

Parse.Cloud.define("sendPubNubMessage", function(request, response) {
    var message = JSON.stringify(request.params.message);
    var PubNubUrl;
    var PubNubKeys;
    Parse.Config.get().then(function(config) {
        PubNubKeys = config.get('PubNubkeys');
    }).then(function() {
        PubNubUrl = 'https://pubsub.pubnub.com/publish/';
        PubNubUrl+= PubNubKeys.Publish_Key + '/';
        PubNubUrl+= PubNubKeys.Subscribe_Key + '/0/';
        PubNubUrl+= request.params.channel +'/0/';
        PubNubUrl+= message;
        return Parse.Cloud.httpRequest({
            url: PubNubUrl,         
            headers: {
                'Content-Type': 'application/json;charset=utf-8'
            }   
        }).then(function(httpResponse) {        
            return httpResponse;
        });
    }).then(function(httpResponse) {
        response.success(httpResponse.text);
    }, function(error) {
        response.error(error);
    });
});

这是另一个示例代码,用于在特定类发生更改后将消息发送到专用 channel :

Parse.Cloud.afterSave("your_class", function(request, response) {
    if (!request.object.existed()) {
        Parse.Cloud.run('sendPubNubMessage', {      
            'message': JSON.stringify({
                'collection': 'sample',
                'objectId': request.object.id
            }),
            'channel' : 'all' // could be request.object.get('user').id
        });
    }
});

关于parse-platform - 解析后台作业可以向当前登录的所有用户发送消息吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31249654/

相关文章:

ios - 使用 Robovm 的 Parse.com crashreporting Symbols

java - 从 Web 服务加载数据后刷新时清除 picasso 图像缓存

javascript - 用我自己的服务器解析云代码返回 "unauthorized"

ios - 解析 : Create 1-to-1 relation on PFObject with support for multiple class types?

ios - 在 Parse iOS 中为每个对象抓取第一张图像

javascript - 解析云代码 Mandrill Promise

parse-platform - 解析云: Query not running in exported function from save() callback

javascript - Parse.com 的 Stripe 云代码模块有限制吗?

ios - 使用 swift 在索引处获取 PFObject

parse-platform - 添加和查询指针不适用于 Parse.com 的 REST API