I'm having difficulty in passing background data to a screen. In the background the app is calling the screen correctly but the data that this screen needs that is in the "data" (id from data object) in the notification is not being picked up.
In foreground I got "data" correctly.
notification json
{
"to" : "akshih890uhnkjh389jfkn3...",
"priority" : "normal",
"time_to_live" : 0,
"data" : {
"type" : "my_type",
"id" : "my_id"
},
"notification" : {
"body" : "Test body",
"title" : "Test title",
"click_action" : ".MyActivity"
}
}
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
String clickAction = remoteMessage.getNotification().getClickAction();
Map<String, String> data = remoteMessage.getData();
Gson gson = new Gson();
MyObject myObject = new MyObject(data.get("id"), data.get("type"), remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody());
Intent intent = new Intent(myObject.getClickAction());
intent.putExtra("id", myObject.getId());
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
sendFCMNotification(title, body, contentIntent);
}
MyActivity
if(getIntent() != null) {
Bundle extras = getIntent().getExtras();
code = extras.getString("id");
}
int id = Integer.valueOf(remoteMessage.getData().get("id"));
Since you use remoteMessage.getData(), you get entire data object, not an object who has data json inside, you get actual data json with your fields.
String data = remoteMessage.getData();
Gson gson = new Gson();
MyObject myObject = gson.fromJson(data, MyObject.class);
If your app is in background, Firebase will not trigger onMessageReceived(). Why.....? I have no idea. In this situation, I do not see any point in implementing FirebaseMessagingService.
According to docs, if you want to process background message arrival, you have to send 'click_action' with your message. But it is not possible if you send message from Firebase console, only via Firebase API. It means you will have to build your own "console" in order to enable marketing people to use it. So, this makes Firebase console also quite useless!
There is really good, promising, idea behind this new tool, but executed badly.
I suppose we will have to wait for new versions and improvements/fixes!
in the background, you can get data from intent extras if you want to call specific activity then specify it in click_action attribute of data
refer:
Firebase Server reference
Firebase Push
I found the problem, when the server sends me the notification object in the background I can not see the date. But when the server sends only the date I get the id however how will I be able to build a notification without the title and body data?
Related
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
I can succesfully send Firebase cloud messages to my Android sample project. As described in here, I apply step 2 and it works so far.
My JSON body:
{
"to": "/topics/testTopic",
"data": {
"key1" : "val1",
"key2" : true
}
}
has a data field which I can access properly in my onMessageReceived() method:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String toVal = remoteMessage.getTo();
Map<String, String> data = remoteMessage.getData();
}
But the getTo() method gets null.
How can I access the 'to' field in my originating JSON?
I was expecting to have "/topics/testTopic" in im variable toVal.
I think you're looking for RemoteMessage.getFrom():
Get the sender of this message.
This will be the sender ID or the topic for topic messages.
I have an Android app which calls a web service to send messages to us for support.
There is a website which we develop where we can respond to a message and we use Firebase for that. Up until now we were just sending a title and a message like so:
var applicationID = "snip";
var senderId = "snip";
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
to = model.RequestFirebaseID,
notification = new
{
body = model.ResponseMessage,
title = model.ResponseTitle
}
};
In the Android app I am able to extract the message title using the following Java code
String notificationTitle = remoteMessage.getNotification().getTitle();
String notificationBody = remoteMessage.getNotification().getBody();
However now I am trying to send back a custom field called id,so I amend the code to be
var data = new
{
to = model.RequestFirebaseID,
notification = new
{
body = model.ResponseMessage,
title = model.ResponseTitle,
requestid = model.id
}
};
So now when I send a message back to the device using Firebase I have this code to try and read the id field.
String messageID = remoteMessage.getData().get("requestid");
However this causes ends up as null.
So I have tried testing sending this via the Firebase console, I add requestid to the custom data section and give it a value and the above code is able to read it.
It seems that when I am sending via the web application it cannot see the requestid field.
You have to include the data message inside the payload. Something like this:
var payload = new
{
to = model.RequestFirebaseID,
notification = new
{
body = model.ResponseMessage,
title = model.ResponseTitle
},
data = new
{
requestid = model.id
}
};
I changed the root variable name to payload to distinguish it.
If you add a custom data when using the Firebase Notifications console to send the message, it is included inside a data message parameter instead of the notification message. See the Message Types documentation for the difference of the two.
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?
Couldn't find any documentation on how to handle push notifications coming from Parse on React Native for Android.
I am able to receive the notification after integrating the SDK, but couldn't go from there in order to handle the notification inside the app.
Any ideas?
No Problem.
This is php code.
$data = array("alert" => 'Hello, this í' );
ParsePush::send(array(
"channels" => ["channels"],
"data" => $data,
"post_id" => '1234',
));
And this is Android code.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String jsonData = extras.getString("com.parse.Data");
JSONObject json = new JSONObject(jsonData);
String postID = json.getString("post_id");
if (postID != null) {
Intent parseIntent = new Intent(this, MatchActivity.class);
parseIntent.putExtra("postID", postID);
startActivity(parseIntent);
}
In android code. You can get parametter post_id from php. Sorry English verry bad