简体   繁体   中英

Push notifications not received when called inside android app - Amazon SNS

I have a problem with my app based on AWS. When I test the following function in Amazon lambda, everything works (I get the push notification on my phone):

console.log("Loading kupa function");
var AWS = require("aws-sdk");

exports.handler = function(event, context) {

    var eventText = JSON.stringify(event, null, 2);
    console.log("Received event:", eventText);
    var sns = new AWS.SNS();
    var params = {
        Message: eventText, 
        Subject: "Test SNS From Lambda",
        TopicArn: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    };
    sns.publish(params, context.done);

    context.succeed("kupa sukces");

};

However, once I use the following method on my phone I get the "kupa sukces" log into my Android Studio but I don't get the notification on the phone. Furthermore, the "Test" on Lambda does not work anymore as wel... Here is the code:

String lambdaRequest = "{\n\"kupa\" : \"" + true + "\"\n}";

        asyncTask.delegate = wysylaczKupy.this;
        asyncTask.friendFunction("friendsRequest",lambdaRequest);
            }

the friendFunction is here:

public static void friendFunction(String funName, String requestContent) {


        final String functionName = funName;
        final String requestPayload = requestContent;

        new AsyncTask<Void, Void, InvokeResult>() {
            @Override
            protected InvokeResult doInBackground(Void... params) {
                try {
                    final ByteBuffer payload =
                            ENCODER.encode(CharBuffer.wrap(requestPayload));

                    final InvokeRequest invokeRequest =
                            new InvokeRequest()
                                    .withFunctionName(functionName)
                                    .withInvocationType(InvocationType.RequestResponse)
                                    .withPayload(payload);

                    final InvokeResult invokeResult =
                            AWSMobileClient
                                    .defaultMobileClient()
                                    .getCloudFunctionClient()
                                    .invoke(invokeRequest);

                    return invokeResult;
                } catch (final Exception e) {
                    Log.e("LAMBDA", "AWS Lambda invocation failed : " + e.getMessage(), e);
                    final InvokeResult result = new InvokeResult();
                    result.setStatusCode(500);
                    result.setFunctionError(e.getMessage());
                    return result;
                }
            }
}

How can I fix this?

Thank you in advance, Jan

Jan,

The Lambda function for publishing to an SNS Topic wasn't quite right. I modified your function and provided a default json value for testing. Just put your TopicARN in and try it out. Once you have tested using the Lambda console, then try the Android code, which I did not try.

Note that when sending a JSON payload to an SNS Topic, a default value is required. The default value is used when you don't specify a protocol specific message. For example, you are publishing to an SNS Topic with Android GCM endpoints and since your JSON payload does not contain "GCM" then all endpoints will receive your default message that you provided.

I'm not sure what you were doing with "{\\n\\"kupa\\" : \\"" + true + "\\"\\n}" but I'm guessing the "kupa": "true" is intended to the the data key/value for the app to handle? If so, you'll need to lookup a proper GCM payload to send both a message and data.

//Pass in the following json for testing: { "default":"some message", "kupa": "true"}
        console.log("Loading kupa function");
        var AWS = require("aws-sdk");

        exports.handler = function(event, context, callback) {
            var eventText = JSON.stringify(event, null, 2);
            console.log("Received event:", eventText);
            var sns = new AWS.SNS();
            var params = {
                Message: eventText,
                MessageStructure: "json",
                Subject: "Test SNS From Lambda",
                TopicArn: "arn:aws:sns:us-west-2:xxxxxxxxxx:test"
            };
            sns.publish(params, function (err, data) {
                if(err){
                    callback(err, null); //failed
                }
                callback(null, "kupa sukces"); //success
            });
        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM