Ad
Amazon-aws : Send Notification Type Of Push
I have a lambda function written and hosted on amazon lambda. Below is the code for that lambda:
const AWS = require('aws-sdk');
exports.handler = (event, context) => {
console.log("Received event:", JSON.stringify(event, null, 2));
const targetArn = event.TargetArn;
const sns = new AWS.SNS();
const payload = {
default: "some default message",
GCM: {
notification: {
title: "Sample title",
body: "Sample Body"
},
data: {
title: "Sample title",
body: "Sample Body"
}
}
};
const params = {
Subject: "some default subject",
Message: JSON.stringify(payload),
MessageStructure: "json",
TargetArn: targetArn
};
console.log('PUBLISHING', JSON.stringify(params, null, 2));
sns.publish(params, function(err, data) {
console.log('PUBLISHED!');
if (err) {
console.log(err, err.stack);
return {
statusCode: 500,
body: JSON.stringify({error: err})
};
} else {
console.log('SUCCESS!', data);
return {
statusCode: 200,
body: JSON.stringify(data)
};
}
});
};
Now, when I test the lambda to test if I receive push on Android, I don't see whole message printed on console. Below is my code I used for logging on Android:
public class MyFirebaseMessagingService extends FirebaseMessagingService implements LifecycleObserver {
public static final String ACTION_USER_FEEDBACK = "ACTION_UserFeedback";
public static final String ARG_TITLE = "Title";
public static final String ARG_BODY = "Body";
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "onMessageReceived() called with: remoteMessage = [" + remoteMessage + "]");
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData() != null) {
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Log.d(TAG, "Data: key, " + key + " value " + value);
}
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
notifyActivity(title, body);
}
if (remoteMessage.getNotification() != null) {
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Log.d(TAG, "Notification: key, " + key + " value " + value);
}
}
}
private void notifyActivity(String title, String body) {
Intent intent = new Intent(ACTION_USER_FEEDBACK);
intent.putExtra(ARG_TITLE, title);
intent.putExtra(ARG_BODY, body);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
@Override
public void onNewToken(String token) {
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
FCMTokenPreference.storeFCMDeviceToken(this, token);
AWSRegistrationIntentService.start(this);
}
}
Below is what I get on console when I test the lambda:
D/MyFirebaseMessagingService: onMessageReceived() called with: remoteMessage = [[email protected]] D/MyFirebaseMessagingService: Data: key, default value some default message
Aim is to send push notification that is of type Notification and not Data
Can anyone help me with this ?
Ad
Answer
Very silly issue but Amazon should have handled it, Anyways.
You have to stringify GCM object in payload object
const payload = {
"default": "User Feedback Request",
"GCM":"{\"notification\":{\"title\":\"Sample title\",\"body\":\"Sample body\"},\"data\":{\"title\":\"Sample title\",\"body\":\"Sample body\"}}"
};
And it works! Damn!
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad