When app is in background, the notification message is deliver by Notification, when the notification is tap, app is launch, how can i get the message body?
the intent is this:
Bundle[{google.sent_time=1470813025421, from=568540028909,
google.message_id=0:1470813025865549%31bd1c9631bd1c96,
collapse_key=com.google.firebase.quickstart.fcm}]
no message body in intent, only message id!
Thanks!
try this
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "From: " + remoteMessage.getData().get("message"));
// Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
// if you sending data with custom key
Log.d(TAG, "Notification Message of custom key: " +remoteMessage.getData().get("your key"));
sendNotification(remoteMessage.getData().get("message"));
}
I am sorry but what you are trying to do is not possible.
Currently it's not possible to access the (body, title, icon ...) information of a
notification-message from the activity that is launched when the notification is opened.
You can instead access the data component of the notification message.
One possible alternative is to use data-message messages and create your own custom notification and custom logic.
see notification-message vs data-message here:
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
PS: if would be useful if you could report a Feature Request through the firebase support page.
In this way the team can correctly prioritize future features.
https://firebase.google.com/support/contact/bugs-features/
Related
I have implemented FCM in my app and getting notification from the action performed in the app. So if a user likes a post I get a notification like "Someone(name) likes you. if multiple users like the post I get multiple notification exes: User1 likes you, User2 likes you.
I want one notification where it shows the Username of the user + number of other user liked you. How to handle this on client side or do we have to handle it from server side. Any ideas?
Firebase cloud messaging will let you send messages to your users in real time. I suggest you read about it from the official google documentation, then implement it so that when someone likes your post, your server is able to send a JSON message to activate Firebase Cloud Messaging.
There are two forms of notification:
Notification messages, sometimes thought of as “display messages.”
These are handled by the FCM (firebase cloud messaging) SDK
automatically.
Data messages, which are handled by the client app.
Step one: Add the FCM SDK API to your project
Step two Create a service that can receive your data message
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "myTag";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
}
Step three: Create your notification from that service
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Someone liked your post")
.setContentText("Accept this answer if it is useful!");
Step four: Handle any "likes" on your server side by sending a JSON request to your Android app with the notification:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", //this specefies the device
"notification":{
"title":"Someone liked your post",
"body":"col"
}
"data": {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
"android":{
"ttl":"86400s",
"notification"{
"click_action":"OPEN_ACTIVITY_1"
}
},
}
}
More about Firebase Cloud Messaging on my blog.
Based on the documentation, it is my understanding that there are two types of messages that Firebase can send: Notification and Data. Also, they can be either collapsible or non-collapsible with non-collapsible being the default for data messages. This would mean that each message is delivered to the client app. See below:
However, when I send data messages to my client they are collapsed. For example, I send one and it appears in the notification bar with no problem but if I don't open it and another message comes in, it is replaced by the new message. Here's some of my code.
Data message:
//create the notification payload
let payload = {
data: {
title: 'Title',
context: context,
parent: parent,
body: user + " commented on your contribution.",
sound: 'default'
}
};
//send the notification
return admin.messaging().sendToDevice(userToken, payload).then(ok =>{
console.log('Notification sent to: ' + submitter);
}).catch(error => {
console.log('Could not send notification.');
});
What am I doing wrong? I want each notification to appear and not be replaced.
You are debugging at the wrong end. The actual problem is on the client side where you are posting the notification.
While posting the notification if the notification id is the same Android OS will replace the existing notification.
Look for the notify() in your code. Refer to this doc for more details : https://developer.android.com/reference/android/app/NotificationManager.html#notify(int, android.app.Notification)
I'm trying to send push notifications throught FCM services.
In my MainActivity I write this code:
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// checking for type intent filter
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
// gcm successfully registered
// now subscribe to `global` topic to receive app wide notifications
FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// new push notification is received
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
}
}
};
In order to subscribe user to a topic.
From backend I make a call to FCM service at this link : https://fcm.googleapis.com/fcm/send
in which I pass this JSON:
{
"to":"/topics/global",
"data":{
"title":"test",
"is_background":false,
"message":"testmessage",
"image":"",
"payload":{
"team":"Test",
"score":"5.6"
},
"timestamp":"2017-05-23 11:55:35"
}
}
and I get this response:
{\"message_id\":8863205901902209389}
But my device doesn't show any notifaction, exept if I use Firebase console with "user segment" or "single device" . Also in Firebase console doesn't works "topic" way.
Thank you in advance for any response.
There are two types of FCM messages.
Notification Messages
Data Messages
On the client side, Notification messages are handled by FCM and automatically displayed in the notification window. If you are using Data Messages, your app needs to handle the received message and create a notification.
The sample payload in your question is a data message and hence it is not displayed in notification (I assume you didn't do any handling). The notifications sent via FCM console are always notification messages and hence those are automatically displayed.
Refer to this FCM page for more details on this.
We require FCM with image,title, and description in notification drawer if app is not in foreground we have try by sending following notification request :
{"priority" : "high",
"to": "/topics/movies",
"data": {
"image":"image ur",
"title":"App name",
"text":"Image with Text"
},"notification":{"title":"App Name","body":"description"}
}
But, in android if app is in background then on messge receive not called however if app is in foreground then notification will generate with image and title
#Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("onMessageReceived-->", "getData ->" + remoteMessage.getData());
}
However if we send by removing notification key then android work if app is in background or foreground. but iOS did not receive if we remove notification key.
Please help us and Thanks in advance.
I migrated from GCM to FCM like instructed here.
Whenever I send a message, the onMessageSent method is not invoked.
I use following source code to send messages:
Map<String,String> data = new HashMap<String,String>();
data.put(GcmConstants.ACTION, GcmConstants.ACTION_CHAT);
data.put(Constants.CHAT_FLAG, Constants.FLAG_NEW_CHAT);
ObjectMapper mapper = new ObjectMapper();
String chatJsonInString = mapper.writeValueAsString(Helper.chatToJson(chat));
data.put(Constants.CHAT_JSON, chatJsonInString);
String receiverJsonInString = mapper.writeValueAsString(Helper.userToJson(receiver));
data.put(Constants.RECEIVER_JSON, receiverJsonInString);
String id = Integer.toString(getNextMsgId(ctxt));
FirebaseMessaging fm = FirebaseMessaging.getInstance();
fm.send(new RemoteMessage.Builder(senderId + "#gcm.googleapis.com").setMessageId(id).setData(data).build());
Why is not working?
If you look at the official website example here, then you will see this comment :
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
// [END_EXCLUDE]
// TODO(developer): Handle FCM messages here.
at the start of onMessageReceived. My understanding from this is that you must have a data component in your message, for the callback to be triggered.
I based my code on this and the callbacks are triggered.
Yes, solved my problem. I put google-services.json into the wrong folder. Next time I should following "get started guide" for implementing the client more precisley.