parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Cloud code push notification request timeout

Open nickjf89-zz opened this issue 10 years ago • 8 comments

I am trying to send a push notification through cloud code on my heroku-based server.

here is my index.js:

var api = new ParseServer({
  databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID || '642k4TTqGk1mOVzIrlTyRgfNlCd5VJ26NtciiC66',
  masterKey: process.env.MASTER_KEY || 'muH3zkWCHTKsxi3LRLTjiFaF2VpPqn7PzMNRbjwv', //Add your master key here. Keep it secret!
  serverURL: process.env.SERVER_URL || 'https://sporter-mobile.herokuapp.com',  // Don't forget to change to https if needed
  liveQuery: {
    classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
  },
  push: {
    ios: [
      {
        pfx: 'aps-dev-Sporter.p12', // Dev PFX or P12
        bundleId: 'uk.co.callistocreations.Sporter',
        production: false // Dev
      },
      {
        pfx: 'aps-prod-Sporter.p12', // Prod PFX or P12
        bundleId: 'uk.co.callistocreations.Sporter',  
        production: true // Prod
      }
    ]
  }
});

And my main.js:

Parse.Cloud.define('test', function(req, res) {
    Parse.Push.send({
      channels: ['user_hrQFsHJXKM'],
      data: {
        alert: 'Test',
        badge: 1,
        sound: 'default'
      }
    }, {
      success: function() {
        console.log('##### PUSH OK');
      },
      error: function(error) {
        console.log('##### PUSH ERROR');
      },
      useMasterKey: true
    });
});

I am able to send push notifications through my parse-server-dashboard that I am using locally, so I know the setup works to that extent.

In my heroku logs, I see:

2016-06-09T15:42:03.365836+00:00 app[web.1]: ##### PUSH ERROR

AND

2016-06-09T15:42:33.207603+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/parse/functions/test" host=sporter-mobile.herokuapp.com request_id=3045d9ee-429a-460d-ba86-59f9dc4655cb fwd="37.46.24.6" dyno=web.1 connect=1ms service=30001ms status=503 bytes=0

nickjf89-zz avatar Jun 09 '16 15:06 nickjf89-zz

Have you tried printing the error to see the actual issue? something like:

      error: function(error) {
        console.log('##### PUSH ERROR');
        console.log(error);
      },

codebreach avatar Jun 09 '16 20:06 codebreach

Also the request timeout is because you never call res.error or res.success thus the server thinks you are still doing some work.

codebreach avatar Jun 09 '16 20:06 codebreach

@codebreach I dont know the problem but are you using an editor that checks for syntax errors? Every problem i've had with parse-server has been syntax related. I failed for 3 months to send pushes and only discovered last week it was purely syntax. So if you are using a texteditor like atom or something, recommend you download something to check for syntax errors.

otymartin avatar Jun 09 '16 20:06 otymartin

You forgot to add res.success and res.error. Because of this, your client never get the responde. You have to do something like this:

Parse.Cloud.define('test', function(req, res) {
    Parse.Push.send({
      channels: ['user_hrQFsHJXKM'],
      data: {
        alert: 'Test',
        badge: 1,
        sound: 'default'
      }
    }, {
      success: function() {
        console.log('##### PUSH OK');
        res.success();
      },
      error: function(error) {
        console.log('##### PUSH ERROR');
        res.error();
      },
      useMasterKey: true
    });
});

davimacedo avatar Jun 09 '16 21:06 davimacedo

Thanks for the tips. I am now getting:

2016-06-09 22:54:44.418 Sporter[1280:569571] [Error]: {
    code = 107;
    message = "Received an error with invalid JSON from Parse: Cannot POST /push\n";
} (Code: 141, Version: 1.12.0)

nickjf89-zz avatar Jun 09 '16 21:06 nickjf89-zz

**UPDATE

Started from scratch.

New Cloud Code function:

Parse.Cloud.define('sendPush', function(request, response) {
    var username = request.params.username;
    var message = request.params.message;

    Parse.Push.send({
        channels: [ request.params.channel ],
        data: {
            alert: message,
            sound: 'default',
            badge: 'Increment'
        }
    }, {
            success: function() {
                    // Push was successful
                    console.log('### PUSH SENT');
                    response.success("### Push sent");
            },
            error: function(error) {
                    // Handle error
                    console.log('### PUSH FAILED');
                    response.error("### Push failed: " + error);
            },
            useMasterKey: true
    });
});

XCode call:

PFUser *currentUser = [PFUser currentUser];
    NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:currentUser.username, @"username",
                            @"This is a test!", @"message",
                            [NSString stringWithFormat:@"user_%@", currentUser.objectId], @"channel",
                            nil];

    [PFCloud callFunctionInBackground:@"sendPush"
                       withParameters:params
                                block:^(NSString *success, NSError *error) {
                                    if (!error) {
                                        NSLog(@"%@", success);
                                        NSLog(@"Push sent: %@", params);
                                    } else {
                                        NSLog(@"%@", error);
                                    }
                                }];

Push is still failing, with this error:

Error Domain=Parse Code=141 "### Push failed: [object Object]" UserInfo={code=141, temporary=0, error=### Push failed: [object Object], NSLocalizedDescription=### Push failed: [object Object]}

Thanks for any help in advance!

nickjf89-zz avatar Jun 10 '16 09:06 nickjf89-zz

_RESOLVED_

It seemed my ServerURL in Heroku was incorrect, however everything else worked previously, and even managed to send push notifications through the Dashboard...Anyway, I added /parse to the end of the environment variable in Heroku, and it's fixed.

nickjf89-zz avatar Jun 10 '16 09:06 nickjf89-zz

@nickjf89 Like i said, syntax errors are always your enemy. I struggled for 5 hours to discover the same problem :)

otymartin avatar Jun 10 '16 16:06 otymartin