Fetching additional data from AWS SNS Push Notification - android

I have a problem regarding my SNS Push Notifications. I have the following lambda code:
db.scan(params, function(err, data) {
if (err) {
console.log(err); // an error occurred
}
else {
data.Items.forEach(function(record) {
var receiverID = record.userDeviceToken.S;
var message = "You have a new invitation to the event";
var topic = "Friend's invitation";
var eventText = JSON.stringify(event);
console.log("Received event:", eventText);
var sns = new AWS.SNS();
var params = {
Message: message,
Subject: "Friend's invitation",
TargetArn: receiverID,
};
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);
}
});
});
//context.succeed(data.Items); // data.Items
}
});
};
Now my goal is to get the "Subject" or "topic" sometimes, if it is possible. I cannot find it in documentation, and I need it to customize my notification title depending on the push message sent (I have few functions).
When I used sample amazon app I found this cound in Push Listener Service:
public static String getMessage(Bundle data) {
// If a push notification is sent as plain text, then the message appears in "default".
// Otherwise it's in the "message" for JSON format.
return data.containsKey("default") ? data.getString("default") : data.getString(
"message", "");
}
This works, but the "data" itself has the following data:
Bundle[{google.sent_time=1480364966070, google.message_id=0:1480364966079787%22269524f9fd7ecd, default=You have a new invitation to the event, collapse_key=do_not_collapse}]
Therefore, it is just providing some internal data and the "message" itself. I cannot access the topic.
My question is: how to get other variables in the Android code so I can use them further on? Can I add custom variables by myself through data bundle?
I have notifications in JSON format. I just wonder, whether the only way is to put the data I want in JSON format inside the message, and then read the message accordingly, or maybe I can attach the required data from lambda function to the push notification already?

Related

Can't parse push payload message in Titanium

In my app, i couldn't parse the "payload" object coming from push notification. There is automatically added '/' in each property in payload after push sent from server. How can i parse the payload property/object and get the notification data in my code?
Here is the payload object :
"payload":"{\"android\":{\"badge\":\"2\",\"alert\":\"Microfinaa_new_ne\",\"sound\":\"door_bell\",\"icon\":\"little_star\",\"vibrate\":true,\"title\":\"Mahboob Zaman\"}}"
And here is the full notification message coming from fcm server:
{"type":"callback","source":{"showTrayNotification":true,"pushType":"gcm","enabled":false,"showTrayNotificationsWhenFocused":false,"singleCallback":false,"focusAppOnPush":false,"showAppOnTrayClick":true,"debug":false,"apiName":"Ti.Module","bubbleParent":true,"invocationAPIs":[],"__propertiesDefined__":true,"_events":{"callback":{}}},"payload":"{\"android\":{\"badge\":\"2\",\"alert\":\"Microfinaa_new_ne\",\"sound\":\"door_bell\",\"icon\":\"little_star\",\"vibrate\":true,\"title\":\"Mahboob Zaman\"}}","bubbles":false,"cancelBubble":false}
And here is my code -
CloudPush.addEventListener('callback', function(evt) {
var json = JSON.stringify(evt.payload);
Ti.API.info("datos = " + json.android);// This line shows undefined
});
Payload is already string you need to parse it and use inverse function
var json = JSON.stringify(evt.payload);
JSON.stringify(Object) -> return String
JSON.parse(StringOject) -> return Object

How to send Push Notification Firebase

I am new to Firebase and the main reason I adapted to it from my old MySql DB is the ability to send push notification and dynamic links. I have been trying for the past two days to send notification to a group of people who have subscribed to a topic from my node.js script. The script always returns InternalServerError. I am able to send notification from the Firebase console but that is not good enough for my app as I need to implement dynamic notification (i.e. triggered by one users action).
So far I did not understand what was in the official docs and tried following a tutorial I found and I am currently here
app.get('/push',function(req,res){
/*var title = req.params.title;
var body = req.params.body;*/
// var confName = req.params.name;
var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
to: '/topics/ilisten',
// collapse_key: 'your_collapse_key',
notification: {
title: 'This is Title',
body: 'This is body'
},
data: { //you can send only notification or only data(or include both)
my_key: 'Conf Name here'
}
};
fcm.send(message, function(err, response){
if (err) {
console.log("Something has gone wrong!"+err);
} else {
console.log("Successfully sent with response: ", response);
}
});
})
My first question is what should I do in the to field so that all the users in my app reciece the notification.
I would also like to take a look at correct and complete implementation of this concept with android code. If anyone has such code please share it here as it would help the future Firebase users who cannot understand the official docs like me.
Following is one approach using node-gcm (https://github.com/ToothlessGear/node-gcm)
var gcm = require('node-gcm');
var sender = new gcm.Sender(<sender_key>);
var message = new gcm.Message();
message.addNotification('title', title);
message.addNotification('body', body);
sender.send(message, { topic: "/topics/" + topic }, function (err, response) {
if (err) console.error(err);
else console.log(response);
});

Get notification when a user wants to add data to the database

I'm about to begin working on a recipe Android app for my college final project, and I want users to be able to add recipes to the database. However, I don't want the data to be added right away, but I'd like to receive a notification whenever someone wants to add a recipe, so I can confirm it myself. I'm using back{4}app by the way.
How can I do such a thing in a not-so-complicated way? I was thinking to create an admin account for myself in the app itself, but is there any way to send the notifications to said account? I also want to be able to confirm recipe addition with a simple "Confirm" button from within the app, so will this require me to create an additional class for pending recipes? Will I need an admin account in any case?
all this can be achieve by using cloud code.
Parse.cloud.define("addRecipe", function(request, response) {
const query = new Parse.Query("recipe");
query.set("name", "name");
query.save({
success function(result) {
response(result);
//call push notification function from client or from cloud code when the error is nil
},
error: function(result, error) {
response(error);
}
});
});
this is an example of push notifications using cloud code.
push notification are not allow anymore from the client due to secure reason.
you should be subscribe to this channel
Parse.Cloud.define("pushsample", function (request, response) {
Parse.Push.send({
channels: ["channelName"],
data: {
title: "Hello!",
message: "Hello from the Cloud Code",
}
}, {
success: function () {
// Push was successful
response.sucess("push sent");
},
error: function (error) {
// Push was unsucessful
response.sucess("error with push: " + error);
},
useMasterKey: true
});
});
you should also implement some logic to your app in order to display recipes confirm by admin.
var recipe = Parse.Object.extend("recipe");
var query = new Parse.Query(recipe);
query.equalTo("confirm", true);
query.find({
success: function(results) {
//it will display recipes confirmed
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
});
you should also setup a admin system in your app or a website

How can i send push notification on any item update event in parse.com

I am using parse for my application
I have one fragment on user which have to write article and save on parse but with not approve
when admin approve that user filed I want to send push notification automatically to that user with particular article is approved message
How can I implement this things..?
Parse.Cloud.afterSave("Data", function(request) {var dirtyKeys = request.object.dirtyKeys();for (var i = 0; i < dirtyKeys.length; ++i) {
var dirtyKey = dirtyKeys[i];
if (dirtyKey === "name") {
//Get value from Data Object
var username = request.object.get("name");
//Set push query
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("name",username);
//Send Push message
Parse.Push.send({
where: pushQuery,
data: {
alert: "Name Updated",
sound: "default"
}
},{
success: function(){
response.success('true');
},
error: function (error) {
response.error(error);
}
});
return; } } response.success();});
If this is a matter of just hitting "approve", I would create a cloud code function for "Approve article" that is called by the admin tapping an approve button. This function sets the approve status and then calls a function (still in cloud code) for sending the push message to the user.
More on cloud code functions: https://parse.com/docs/cloudcode/guide#cloud-code-cloud-functionshttps://parse.com/docs/cloudcode/guide#cloud-code-cloud-functions
Alternatively the approve button can change status and save the document, and then an afterSave() function in cloud code can handle the push. This is less clear, though, since the afterSave() function will always be called when the record is saved, and it would need to check for status and only send push if the article has been approved.

Is it possible to send additional data with parse push notifications using cloud?

Is it possible to put extra data into your push notification using cloud, that would not show up in your notification bar but you could still pull it from the notification? It would look something like this:
Parse.Push.send({
where : pushQuery,
data: {
alert : "You have a new reminder!"
// *** HERE ***
// extraData : someData (It would be a, String)
}
}).then(function() {
response.success("Push was sent successfully!")
}, function(error) {
response.error("Push failed to send with error: " + error.message);
});
Yes, you can. You can add additional data to the dictionary, just like the "alert" example:
Parse.Push.send({
where : pushQuery,
data: {
alert : "You have a new reminder!",
// *** HERE ***
// extraData : someData (It would be a, String)
// Extra data:
title: "Some title text",
yourMessage: "Some message text",
objectId1: "Some object id"
}
}).then(function() {
response.success("Push was sent successfully!")
}, function(error) {
response.error("Push failed to send with error: " + error.message);
});
You can also add object ids which you can use to retrieve objects after receiving the notification.
Here is a tutorial that shows what to do: http://androidbook.com/akc/display?url=DisplayNoteIMPURL&reportId=4553&ownerUserId=android
JSONObject data = getJSONDataMessageForIntent();
ParsePush push = new ParsePush();
push.setChannel("ch1");
push.setData(data);
push.sendInBackground();

Categories

Resources