我是 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/