Parse.Cloud.afterSave用于发送无法从Heroku服务器

时间:2016-03-22 19:59:48

标签: ios heroku push-notification migration parse-server

我做了迁移,从Parse到Heroku,一切顺利。此时我没有在Parse上设置任何云代码或推送通知。当我完成迁移后,我开始实现云代码以在每次保存新文本消息时处理推送通知,但Parse.Cloud.afterSave方法未运行。我知道main.js正在工作,因为如果我实现

Parse.Cloud.define('hello', function(req, res) {
res.success('Hi');
});

我得到了#34;嗨"在Xcode中返回我的控制台,但由于某种原因,Parse.Cloud.afterSave什么都不做。我也知道我的Parse.Cloud.afterSave代码是正确的,因为它通过解析我在另一个应用程序上使用相同的代码。我错过了什么?

这是我的index.js文件

var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var path = require('path');

var databaseUri = process.env.DATABASE_URI || process.env.MONGOLAB_URI;

if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}

var api = new ParseServer({
databaseURI: databaseUri || '',

cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || '',
masterKey: process.env.MASTER_KEY || '',
fileKey: 'XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', // file key from parse
serverURL: process.env.SERVER_URL || '',

push: {
ios: {
pfx: '/Users/alexcruz/Desktop/Certificates.p12', // The filename of private key and certificate in PFX or PKCS12 format from disk
bundleId: 'com.daps.DapsPush', // The bundle identifier associate with your app
production: false // Specifies which environment to connect to: Production (if true) or Sandbox
}
}
});

var app = express();

// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));

// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);

// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
res.status(200).send('Make sure to star the parse-server repo on GitHub!');
});

// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/test', function(req, res) {
res.sendFile(path.join(__dirname, '/public/test.html'));
});

var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
console.log('parse-server-example running on port ' + port + '.');
});

// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);

这是我的main.js文件

//Push Notification for messages that are received
Parse.Cloud.afterSave("MXTMessage", function(request) {
  var messageText = request.object.get('message');
  var usersReceived = request.object.get('receiver');
  var currentUser = Parse.User.current();
  var pushQuery = new Parse.Query(Parse.Installation);
      pushQuery.equalTo('user', usersReceived);
      pushQuery.notEqualTo('user', currentUser);
        Parse.Push.send({
            where: pushQuery, // Set our Installation query
            data: {
              alert: "New message: " + messageText
             }
            }, {
      success: function() {
      // Push was successful
          },
      error: function(error) {
        throw "Got an error " + error.code + " : " + error.message;
          }
            });
});

谢谢

1 个答案:

答案 0 :(得分:0)

由于我不被允许发表评论,我将不得不将其作为回答,但只是将其视为评论:

您是否验证了函数本身实际上从未被调用,例如通过在函数的开头添加一个console.log,或者可能是push本身不起作用(你可以通过REST API调用发送推送来检查)?例如,push似乎在parse-server 2.2.0中被破坏,我不得不降级到1.6.0以使其正常工作。

解决方法可能是创建一个用于发送推送的云代码函数,并在邮件保存在服务器上后由客户端显式调用此函数。

此外,您应该检查:https://github.com/ParsePlatform/parse-server/issues/301,因为您在代码中使用了Parse.User.current()。

另外,我觉得parse-server目前还不是很稳定。我使用parse-server和Heroku遇到很多错误,因为在parse.com上运行的应用程序没有。