To send a message from FCM backend, we have this view :
I wanna to get the message label (libellé du message) from RemoteMessage. When I debug for the field, the content in the message is labeled as google.c.a.c_l, which I think is an internal field.
First, I just wonder the same as your question. But after read the document here and played around with it, I found that:
You cannot get the message label on client device, because it is just a label for display on firebase console only.
To get title at client side, you have to use Advanced options which is auto collapse at the bottom. With Advanced options, you can also send a data payload to the client by key/value.
Then remoteMessage.getNotification().getBody() for notification's message body (from Message text/Texte du message), remoteMessage.getNotification().getTitle() for notitifcation's title (from Advanced options), remoteMessage.getData() for data payload (from Advanced options).
Try to get it with remoteMessage.getNotification().getTitle(). However, check before if remoteMessage.getNotification() != null.
Hope this helps.
Related
I have made an android app (Android Studio / Java) that checks a website for content and stores it in an sqlite DB. If new content is fetched that is not stored in DB, it shows a notification with the new content for user to notice.
That's working fine, although if user does not read/open/dismiss that notification, the next notification will update current one and replace its content with new data. This is wanted behavior, because I don't want the user to receive many notifications for the same thing, so I'm using the same notification id.
This introduces a problem though, if user checks the notification now, he will see the second fetched data, but won't be aware of the existence of the first fetched data.
So, what I'm trying to do is to append to the notification's content, so that both first and second fetched data are shown.
I tried the "inboxStyle" notifications that allow for new lines to be added, but it seems to be working only for setting many lines at the time notification is created and not for appending lines to an existing notifications.
I know that I can do that by storing what user has seen and what not, whether a notification was opened, etc, but this seems too much hassle for a simple thing, there must be an easier way to achieve it.
The expected behavior would be to either be able to append the message of existing notifications, or be able to fetch the message of an existing notification (by id) and then manually append to it and push the updated notification.
If that's not clear enough, the expected outcome is:
Issue the first notification with message "Test message 1"
Issue second notification using the same notification-id with message "Test message 2" that would NOT overwrite "Test message 1" but rather keep that message and append to it, so that the notification's message would now be "Test message 1 {newline-here} Test message 2" (or even better reversed so that the last message is shown on top).
Thank you in advance!
I was looking for a solution myself. I managed to do something that works but I'm quite sure there are other elegant solutions : I'm checking whether or not similar notification has been displayed. If so, I get the previous content and append it to the new notification content before publishing.
First, make sure that the notifications you want to assemble have the same uniqueID
This way, when you receive your 2nd, 3rd notification, you can easily match the one you are creating with the ones already displayed.
Note : This code works only on API 23 and higher.
String message = null;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
for (StatusBarNotification notification: notifications) {
if (notification.getId() == uniqueID) {
// You have a match
Bundle extras = notification.getNotification().extras;
message = extras.getCharSequence(Notification.EXTRA_TEXT).toString();
break;
}
}
}
The easy part : Now that you have the previous notification text, you have to append it to the new notification text before publishing
String newMessage = message + remoteMessage.getData.get("text");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),
channelID)
.setStyle(new NotificationCompat.BigTextStyle().bigText(newMessage))
.setContentTitle("Title")
.setContentText(notification.getMessage())
.setAutoCancel(true);
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)
We are using Firebase in a SIP app to send us missed call notifications and chat notifications whenever the app was offline.
While sending and receiving works fine, we have the effect on the Android client, that 5 missed calls obv produce 5 missed-call-notifications, filling up the notification bar on the client device.
How can we merge those notifications together, to just show a single "5 missed calls" notification?
Is there any additional flag (like grouping) we can put in the data or notification part of the message?
Here is an example of our current missed call notification:
{
"to":"<<FCMToken>>",
"priority":"high",
"notification":{
"title":"<<Displayname-of-Caller>>",
"text":"<<Date-and-time-of-call>>",
"icon":"icon_notification_missed",
"click_action":"MISSED_CALL"
},
"data":{
"type":"sip-call-missed"
}
}
So what's the trick in combining them together as one?
We found the correct solution.
There are more existing keywords for the notification content.
The one we needed was "tag".
We can even localize the client-side text of the notification by supplying a resource name in loc keys.
Here is a correct message that can be bundled together:
{
"to":"<<FCMToken>>",
"priority":"high",
"notification":{
"title_loc_key":"notification_missed_call",
"tag":"MISSED_CALL",
"body_loc_key":"notification_missed_call_multiple",
"body_loc_args":["<<missed_call_count>>"],
"icon":"icon_nav_main_chat",
"click_action":"MISSED_CALL"
},
"data":{
"type":"sip-call-missed"
}
}
The tag will be merged by the client ... say: they will replace each other. Whenever a notification with a tag arrives, it replaces all other existing notifications with the same tag.
So the trick here is, to supply a running count <<missed_call_count>> (which the server has to count), so the client can show an increasing number, like "5 missed calls".
The string "%d missed calls" is stored in the client side string resource named "notification_missed_call_multiple".
I am using the Gmail API for Android for an application to send emails.
I want to send the mails such that they are received in the Social group of messages.
So is it possible in any way that I can set the labels for an email while sending it using the Gmail API ?
It is possible for us to set labels while sending mails through mail.google.com so how can the same be achieved with the Gmail API ?
You cannot specify the labels message should have when sending it. Under what tab certain messages should end up under is up to the user.
But if you are sending a message to the user himself howewer, it's not that hard to modify the message once it has been sent. When you send a message, you get all the label that were applied to the message in the response. Just send a message, and then modify the labels, and you are done.
Here's an example (with regular http-requests, but you could do the same with one of the client libraries):
// Base64-encoding the message and making it url-safe by replacing
// all '+' with '-', and all '/' with '_'.
btoa("To: example#gmail.com\n" +
"From: example#gmail.com\n" +
"Subject: Cool man\n\n" +
"Here is the message").replace(/\+/g, '-').replace(/\//g, '_');
// => "VG86IGV4YW1wbGVAZ21haWwuY29tCkZyb206IGV4YW1wbGVAZ21haWwuY29tClN1YmplY3Q6IFRoaXMgaXMgdGhlIHN1YmplY3QKCkhlcmUgaXMgd2VyZSB0aGUgbWVzc2FnZSBnb2Vz"
Sending the message:
POST https://www.googleapis.com/gmail/v1/users/me/messages/send
{
"raw": "VG86IGV4YW1wbGVAZ21haWwuY29tCkZyb206IGV4YW1wbGVAZ21haWwuY29tClN1YmplY3Q6IFRoaXMgaXMgdGhlIHN1YmplY3QKCkhlcmUgaXMgd2VyZSB0aGUgbWVzc2FnZSBnb2Vz"
}
Response
{
"id": "150866b2f6956617",
"threadId": "150866b2f6956617",
"labelIds": [
"SENT",
"INBOX",
"UNREAD"
]
}
Then, I just the add the CATEGORY_SOCIAL-label to get it to show under the social-tab (removing the INBOX-label will not show it at all).
Request
POST https://www.googleapis.com/gmail/v1/users/me/messages/150866b2f6956617/modify
{
"addLabelIds": [
"CATEGORY_SOCIAL"
]
}
Worked great!
I know that this may possible be a duplicate question, but the other questions don't answer mine.
I am working on an app that pulls notifications (specifically notifications for new WhatsApp messages) from the status bar and reads their content. I have managed to pull the notification title and the message content.
The problem is, when more than one unread message is received, the notification switches from using EXTRA_TEXT to EXTRA_SUMMARY_TEXT (then returning e.g. "2 new messages" instead.
It must possible to separate the messages somehow, seeing as certain existing apps do it (e.g. Snowball combines all messages and displays them in one place, showing message content even when multiple messages have been received and are still unread).
I know that users can send messages via Intents. However, I cannot seem to access incoming intents and have, therefore, assumed that WhatsApp uses explicit intents to send messages.
Intent i = new Intent("com.test.testapp.NOTIFICATION_LISTENER");
Bundle extras = sbn.getNotification().extras;
if(sbn.getPackageName().contains("com.whatsapp"))
{
String title = extras.getString(Notification.EXTRA_TITLE);
String summary = extras.getString(Notification.EXTRA_SUMMARY_TEXT);
String msg = extras.getString(Notification.EXTRA_TEXT);
if(msg != null)
{
i.putExtra("notification_event", msg);
}
else
{
i.putExtra("notification_event", summary);
}
}
else
{
i.putExtra("notification_event","...");
}
sendBroadcast(i);
My question:
How can I display all received messages without getting "2 new messages" as content OR is there a better way of doing this?
I need to access message content, the sender's number and the time that the message was received, so that I can save it to a database.
Any help would be appreciated.
WhatsApp application has structure for sending notification like this :
Case Notification
Message comes from A : Hi Title : A Text: Hi
Message comes from A : How are you Title : A Text: How are you
Title : A Text: 2 new messages
Message comes from B : Hello Title : B Text: Hello
Title : B Text: 1 new message
Title : A Text: 2 new messages
Title : WhatsApp Text: 3 new messages from 2 conversation
---- Here comes the stacking ----
Message comes from C : Good work Title : C Text: Good work
Title : C Text: 1 new message
Title : B Text: 1 new message
Title : A Text: 2 new messages
Title : WhatsApp Text: 4 new messages from 3 conversation
---- This way when new sender message comes, previoud notifications also comes and we get callback in NotificationListener ----
Last notification comes with Title as Package Name : WhatsApp and Text as : X messages from Y Conversation
To get Text :
sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT).toString();
To get Title :
sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TITLE).toString();
To work with this sturcture of stacking, we need to parse this notification stack and display only selective information in our application
I hope my answer will help and solve your query
This answer is given at : enter link description here