I want to send push notifications in my lambda function. It working as expected on localhost but when I push it to lambda code does not working.
Here is my code:
const FCM = require('fcm-node');
let sendNotification = function(regToken) {
let serverKey = 'MY_SERVER_KEY';
let fcm = new FCM(serverKey);
let message = {
to: regToken,
notification: {
title: 'Title of push notification',
body: 'Body of push notification'
}
};
fcm.send(message, function(err, response){
if (err) {
console.log("Something has gone wrong!", err);
} else {
console.log("Successfully sent with response: ", response);
}
});
}
You need to encapsulate functions in the exports.handler
Related
Getting duplicate push notifications while sending one push notification through AWS Pinpoint Service using FCM channel pinpoint.sendMessage method.
pushing notification on an FCM token but getting two different request IDs.
Push Notification callback Reponse
Why does this happen?
How can stop to send duplicate notification from node server?. and I am using "aws-sdk": "^2.1266.0". Any help would be appreciated.
code sample to send push notification :
export const sendPushNotification = async (userId: number, payload: RealTimeNotificationPayload) => {
const deviceTokens = await getAllDeviceTokensForUser(userId);
logInfo('deviceTokens==', deviceTokens)
const tokensTobeRemoved = [];
const pushNotificationPromises = deviceTokens.map(async (deviceToken) => {
const pinpoint = new AWS.Pinpoint();
const params = {
ApplicationId: commonConfig.aws.pinPointApplicationId,
MessageRequest: {
Addresses: {
[deviceToken]: {
ChannelType: PushNotifications.ChannelType.GCM
}
},
MessageConfiguration: {
GCMMessage: {
RawContent: JSON.stringify({
notification: {
title: "Title",
body: "Test body"
}
}),
Priority: 'medium',
SilentPush: false,
TimeToLive: 30,
} as AWS.Pinpoint.GCMMessage
}
}
} as AWS.Pinpoint.Types.SendMessagesRequest;
logInfo('params.MessageRequest==', params.MessageRequest.MessageConfiguration)
await pinpoint.sendMessages(params, async function (err, data) {
if (err) {
logError(err, `Error while sending push notification device token ${deviceToken}`);
}
else {
if (checkIfTokenInvalid(deviceToken, data)) {
tokensTobeRemoved.push(deviceToken);
}
console.log("Response data==> ", JSON.stringify(data, null, 4));
logInfo('data==', deviceToken, data.MessageResponse.Result[deviceToken].DeliveryStatus)
}
}).promise();
})
await Promise.all(pushNotificationPromises);
logInfo('tokensTobeRemoved==', tokensTobeRemoved)
if (tokensTobeRemoved.length) {
await deleteInvalidDeviceTokens(tokensTobeRemoved);
}
}
I am having a bit of an issue getting push notifications to be sent remotely from a lambda using AWS pinpoint.
In my application I have the following code:
PushNotification.configure(awsconfig)
PushNotificationIOS.addEventListener('registrationError', console.log)
PushNotification.onRegister(async token => {
console.log('in app registration', token)
PushNotification.updateEndpoint(token)
setRegistrationToken(token)
})
PushNotification.onNotification(notification => {
console.log('in app notification', notification)
if (Platform.OS === 'ios') {
notification.finish(PushNotificationIOS.FetchResult.NoData)
}
})
PushNotification.onNotificationOpened(notification => {
console.log('the notification is opened', notification)
})
const endpointId = Analytics.getPluggable('AWSPinpoint')._config.endpointId
console.log(`endpoint ID: ${endpointId}`)
if (Platform.OS === 'ios') {
PushNotification.requestIOSPermissions()
}
That code runs perfectly and I can take the token that is logged, go to the pinpoint console, and send a "Test Notification" by choosing "Device Tokens" and "APNS Sandbox."
After that, I set up my lambda and added the following code:
const AWS = require('aws-sdk');
async function sendTestNotification() {
console.log("CALLED SEND TEST")
const title = "Test Message"
const message = "This is a message from Pinpoint dynamically delivered via code"
const applicationId = <HARD CODED APPLICATION ID FROM PINPOINT CONSOLE>
const token = <HARD CODED DEVICE TOKEN I USED IN THE TEST NOTIFICATION>
var messageRequest = {
'Addresses': {
[token]: {
'ChannelType' : 'APNS_SANDBOX'
}
},
'MessageConfiguration': {
'APNSMessage': {
'Action': 'OPEN_APP',
'Body': message,
'Priority': 'normal',
'SilentPush': false,
'Title': title,
'TimeToLive': 30
}
}
};
AWS.config.update({ region: 'us-east-1'});
var pinpoint = new AWS.Pinpoint();
var params = {
"ApplicationId": applicationId,
"MessageRequest": messageRequest
}
pinpoint.sendMessages(params, (sendMessagesErr, sendMessagesData) => console.log(sendMessagesErr, sendMessagesData))
}
exports.handler = async (event) => {
console.log('running')
await sendTestNotification();
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
When I test the function, it runs and completes normally with no errors. However, no notifications come through. Initially, I thought it may have to do with permissions, but I have given the lambda full access to Pinpoint via a custom role, and that still did not change the result.
Any help would be much appreciated, as I am not sure where to troubleshoot next.
You haven't set the token in your MessageRequest. You have [token] under addresses:. You need to put the actual APNS token here. (I've put in some random letters in numbers here for an example...
var messageRequest = {
'Addresses': {
'483hgto483gh87egf8o2phqradgq3984hgeorigheg27r': {
'ChannelType' : 'APNS_SANDBOX'
}
},
'MessageConfiguration': {
'APNSMessage': {
'Action': 'OPEN_APP',
'Body': message,
'Priority': 'normal',
'SilentPush': false,
'Title': title,
'TimeToLive': 30
}
}
};
I have a SNS mobile push notification service configured in AWS with FCM. I am able to send push notifications to a particular endpoint from the console with publish message.
But I want to publish a message to a particular endpoint from a Lambda function. I wrote a Lambda and gave necessary functions. But when I try to run the lambda there is no error thrown and no push notification actions.
Here is my code,
var AWS = require('aws-sdk');
var sns = new AWS.SNS();
exports.handler = async (event) => {
var params = {
TargetArn:'arn:aws:sns:eu-central-1:<my_user_id>:app/GCM/<my_sns_projectname>/<device-token>',
Message:'Success!!! ',
Subject: 'TestSNS'
};
await sns.publish(params , (err,data)=>{
if (err) {
console.log("Error sending a message " + err);
} else {
console.log("Sent message: " + data.MessageId);
}
})
};
I am a beginner, some help pls :)
Voila!! This worked for me!
var AWS = require('aws-sdk');
var sns = new AWS.SNS();
const params = {
TargetArn:'arn:aws:sns:eu-central-1:<my_user_id>:app/GCM/<my_sns_projectname>/<device-token>',
Message:JSON.stringify('Success!!! '),
Subject: 'TestSNS'
};
exports.handler = async (event, context, callback) => {
const publishPromise = sns.publish(params).promise();
await publishPromise.then((response) => {
console.log("SENT")
callback(null, "Message published.");
}, (err) => {
console.log("We had an error");
console.log(err);
callback("Message wasn’t able to publish.");
});
};
I have used a very simple code slightly modified from AWS provided example:
exports.handler = async (event) => {
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set region
AWS.config.update({region: 'ap-southeast-2'});
// Create publish parameters
var params = {
Message: 'This is a sample message',
Subject: 'Test SNS From Lambda',
TopicArn: 'arn:aws:sns:ap-southeast-2:577913011449:TestTopic'
};
// Create promise and SNS service object
var publishTextPromise = new AWS.SNS().publish(params).promise();
let response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
// Handle promise's fulfilled/rejected states
publishTextPromise.then(
function(data) {
console.log("Message ${params.Message} send sent to the topic ${params.TopicArn}");
console.log("MessageID is " + data.MessageId);
response.result = 'Success';
}).catch(
function(err) {
console.error(err, err.stack);
response.result = 'Error';
});
return response;
};
And I am getting a timeout error when testing this service. 3 seconds is the limit.
Since this is a very simply process, I suppose it shouldn't take more than 3 seconds to execute.
I have checked my IAM setting and grant my profile (admin profile) to have full access to SNS service. But the error still persists. I am wondering what is being wrong here and how should I fix this?
I am not sure why you're getting the timeout, but your code isn't supposed to work the way you might expect.
See that you're returning your response outside your .then() code, meaning that your code is going to return before your .then() code is even run (Promises are asynchronous).
Since you're already using Node 8, you're better off using async/await instead of using the old .then().catch() approach.
I have refactored your code a little bit and it works just fine. I have kept the original parameters for your convenience. See how the code is much easier to read and debug.
'use strict';
// Load the AWS SDK for Node.js
const AWS = require('aws-sdk');
// Set region
AWS.config.update({region: 'ap-southeast-2'});
const sns = new AWS.SNS()
module.exports.handler = async (event) => {
const params = {
Message: 'This is a sample message',
Subject: 'Test SNS From Lambda',
TopicArn: 'arn:aws:sns:ap-southeast-2:577913011449:TestTopic'
};
let response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
try {
const data = await sns.publish(params).promise();
response.messageId = data.MessageId,
response.result = 'Success'
} catch (e) {
console.log(e.stack)
response.result = 'Error'
}
return response
};
If for whatever reason you don't want to use async/await, you then need to move the return of your function inside your .then() code, as well as return as soon as the promise is invoked, like this:
'use strict';
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set region
AWS.config.update({region: 'ap-southeast-2''});
// Create publish parameters
var params = {
Message: 'This is a sample message',
Subject: 'Test SNS From Lambda',
TopicArn: 'arn:aws:sns:ap-southeast-2:577913011449:TestTopic'
};
module.exports.handler = async (event) => {
// Create promise and SNS service object
var publishTextPromise = new AWS.SNS().publish(params).promise();
let response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
// Handle promise's fulfilled/rejected states
return publishTextPromise.then(
function(data) {
console.log("Message ${params.Message} send sent to the topic ${params.TopicArn}");
console.log("MessageID is " + data.MessageId);
response.result = 'Success';
return response;
}).catch(
function(err) {
console.error(err, err.stack);
response.result = 'Error';
return response
});
};
I highly recommend you go with approach #1 though.
I have a problem with my lambda function. It takes the data of the devices and sends the push notification (information that someone added you to friends). However, it often happens that the push notificaiton is not sent. Sometimes, I have to activate the function couple of times (through simultaneous clicking on the button) to make it send push notifications. When testing the below function in Lambda it does not send any push notifications. What might be the issue?
Thank you in advance,
John
console.log("Loading friend-request function");
var AWS = require("aws-sdk");
exports.handler = function(event, context) {
var senderID = event.senderID;
var receiverID = event.receiverID;
var message = event.message;
var eventText = JSON.stringify(event);
console.log("Received event:", eventText);
var sns = new AWS.SNS();
var params = {
Message: message,
Subject: "Test SNS From Lambda",
TargetArn: receiverID
};
context.succeed(message);
sns.publish(params, context.done);
};
Remember that the sns.publish() function is asynchronous, i.e. you must wait for it to complete before you can call the context.succeed(). Otherwise, the Lambda function may terminate before the message has been published.
exports.handler = function(event, context) {
// same as before...
var params = {...}
// do not call context.succeed() yet
sns.publish(params, function(err, data) {
if (err) {
console.log('Failed to publish SNS message');
context.fail(err);
}
else {
console.log('SNS message published successfully');
context.succeed(data);
}
});
};