parse-server-modules / parse-server-sns-adapter

Parse Server SNS Adapter
18 stars 14 forks source link

Parse Push send error #11

Closed GweebarraRover closed 7 years ago

GweebarraRover commented 7 years ago

I have set up everything correctly in the index file. According to the documentation.

In the main.js I have

var pushConfig =  { pushTypes : { android: {ARN : 'the_android_arn'},
                                  ios: {ARN:'the_ios_arn', production: true, bundleId: "this_is_right_too"}
                                 },
                   accessKey: 'accessKey',
                   secretKey: 'secretKey',
                   region: 'eu-west-1'
                 };

var SNSPushAdapter = require('parse-server-sns-adapter');
var snsPushAdapter = new SNSPushAdapter(pushConfig);
pushConfig['adapter'] = snsPushAdapter;

I have replaced the values of the ARN's and access keys above with the actual values. I then use the adapter in the ParseServer init like so

'push: pushConfig'

After deploying there is no errors. Although when I call cloud code below

Parse.Cloud.define("sendTestPush", function(request, response) {

    Parse.Cloud.useMasterKey();

    console.log('Trying to send test push');

    console.log('Request');
    console.log(request);

      var user = request.params.user

    console.log("User:");
    console.log(user);

      //Set push query
      var pushQuery = new Parse.Query(Parse.Installation);
      pushQuery.equalTo("user",user);

    console.log("Push query:");
    console.log(pushQuery);

    Parse.Push.send({
        where: pushQuery, // Set our Installation query
        data: {
            alert: "Test push",
            badge: "1",
            sound: "default"
        }
    },
    {
       useMasterKey: true
    },
    {
        success: function () {
            response.success("Success");
        },
        error: function (error) {
            // Handle error
        }
    });
});

I get the following error in the logs

Error generating response. ParseError { code: 115, message: 'Missing push configuration' } code=115, message=Missing push configuration'

Is there something I'm missing?

nitrag commented 7 years ago

You need to do some more research on the docs.

Parse.Cloud.useMasterKey(); //deprecated
Parse.Push.send({
        where: pushQuery, // Set our Installation query
        data: {
            alert: "Test push",
            badge: "1",
            sound: "default"
        }
    },
    {
       useMasterKey: true
    },
    {
        success: function () {
            response.success("Success");
        },
        error: function (error) {
            // Handle error
        }
    });

should be

Parse.Push.send({
        where: pushQuery, // Set our Installation query
        data: {
            alert: "Test push",
            badge: "1",
            sound: "default"
        }
    },
    {
       useMasterKey: true,
       success: function () {
            response.success("Success");
        },
        error: function (error) {
            // Handle error
        }
    });

and your main.js is not correct.

  databaseURI: databaseUri,
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  pushAdapter: pushConfig, //add this
GweebarraRover commented 7 years ago

Interesting, I didn't get any errors on the syntax. Is there actual documentation on how to use the sns adapter as I could not find any. Could you post a link to them?

I have applied your edits although I am still receiving the same error in the logs

error: Error generating response. ParseError { code: 115, message: 'Missing push configuration' } code=115, message=Missing push configuration
[object Object]

The response from the cloud function to the phone is

The data couldn’t be read because it isn’t in the correct format.

I'm not sure if this is of any use

nitrag commented 7 years ago

It's in the README of this repo: https://github.com/parse-server-modules/parse-server-sns-adapter

You then need to instantiate the ParseServer info with the following:

var api = new ParseServer({

  push: pushConfig
});
GweebarraRover commented 7 years ago

To be honest the readme doesn't say I should add

pushAdapter: pushConfig, //add this

anywhere, so am I right in saying that when I instantiate ParseServer, I have both pushAdapter & push? like so

  pushAdapter: pushConfig,
  push: pushConfig

After adding these I still have the same error. Im assuming the error is telling me that the configuration is not set rather than incorrectly set?

Is there something I'm missing in the Cloud code file? That should be defined outside the functions

nitrag commented 7 years ago

Maybe they changed the config again... https://github.com/ParsePlatform/parse-server/wiki/Push

try:

var server = new ParseServer({
  databaseURI: '...',
  cloud: '...',
  appId: '...',
  masterKey: '...',
  push: {
    adapter: pushConfig //or: snsPushAdapter
  }
});
GweebarraRover commented 7 years ago

Unfortunately neither of these work. Which would explain why the readme states

var SNSPushAdapter = require('parse-server-sns-adapter');
var snsPushAdapter = new SNSPushAdapter(pushConfig);
pushConfig['adapter'] = snsPushAdapter;

And then the push adapter is supposed to be set up using

'push: pushConfig'

I am really confused about this, I actually wonder if anyone is using this adapter. Or if it even works. I may just have to return to the the default Parse push or use OneSIgnal or something

nitrag commented 7 years ago

I am using it, it works. Repost your modified Cloud Code function please.

GweebarraRover commented 7 years ago

Ok here it is

Parse.Cloud.define("sendTestPush", function(request, response) {

    //Parse.Cloud.useMasterKey();

    console.log('Trying to send test push');

    console.log('Request');
    console.log(request);

      var user = request.params.user

    console.log("User:");
    console.log(user);

      //Set push query
      var pushQuery = new Parse.Query(Parse.Installation);
      pushQuery.equalTo("user",user);

    console.log("Push query:");
    console.log(pushQuery);

    Parse.Push.send({
        where: pushQuery, // Set our Installation query
        data: {
            alert: "Test push",
            badge: "1",
            sound: "default"
        }
    },
    {
       useMasterKey: true,
       success: function () {
            response.success("Success");
        },
        error: function (error) {
            // Handle error
        }
    });
});

I can see everything in the logs right up until the Parse.Push.send, thats where the configuration error appears. Is there definitely no other variables I need outside of the function? Something similar to Stripe has? That allows me to use stripe. like

var Stripe = require('stripe'); 
var stripe = require('stripe')('sk_test_DCvRkobQAEjZfQQy44dsTesT');
nitrag commented 7 years ago

No, I think you have all the config right. Last step I can think to troubleshoot is the ParseServer options themselves.

var opts = {
  databaseURI: databaseUri,
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  ....
};
console.log(JSON.stringify(opts));
var api = new ParseServer(opts);

Do the above and check the console output when you restart parse-server. Paste it into JSONlint and verify all your config.

GweebarraRover commented 7 years ago

This is a real Homer Simpson Doh moment!

I followed your advice and printed the options and low and behold the push config didnt appear. After investigating I found I declared the push config after I instantiated the parse server.

Thanks again for your help.