I have created application for receieving the message from one perticular number.Its working fine.Now i want to display the alert icon on inbox application icon after receiving the message.Where should i add the code for that.
if ( extras != null ) {
// get array data from SMS
Object[] smsExtra = (Object[]) extras.get( "pdus" ); // "pdus" is the key
for ( int i = 0; i < smsExtra.length; ++i ) {
// get sms message
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
// get content and number
String body = sms.getMessageBody();
String address = sms.getOriginatingAddress();
// create display message
if( address.equals("+91999999999")){
messages += "SMS from " + address + " :\n";
messages += body + "\n";
// notify new arriving message
Toast.makeText( context, messages, Toast.LENGTH_LONG ).show();
listSms.add(new SmsInfo(address, body));
this.abortBroadcast();
}
I am sure where you want to display alert badge/icon, but you can check and try to implement: https://github.com/jgilfelt/android-viewbadger
The icon is hardcoded in the AndroidManifest file. It is designed in this way, so the app icon can be retrieved without running any piece of code (which may result in slower startup for the app)
Some custom home applications support this, but with private APIs.
You can try to use the NotificationManager and add an icon to the status bar. That would be even more visible, which is a more recommended way of doing it. Use the status bar in such cases, for what's its designed for...
In regards to getting a dynamic icon, when the phone receives the message (push notification or whatever) and your app goes to build the notification, you can do a small http request to pull a dynamic icon file before building the notification. This will delay the posting a few seconds, but it doesn't really matter. Then you can get the resulting image and use it in the notification. The specific image can be chosen by sending some data with the (let me guess, push notification) that identifies what photo, such as a url, or an id that you can append onto an already existing url in your notification building part of the app.
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);
When a bot notification is sent to the user on mobile containing an attachment it mentions "[Bot Name] sent you a card" as follows:
The associated activity object in code is as follows:
Activity activity = new Activity()
{
Timestamp = DateTime.UtcNow,
Type = "message",
ChannelId = "msteams",
From = bot,
Recipient = user,
Summary = "Test Notification!",
Attachments = new List<Attachment>() { cardAttachment } //cardAttachment is an object of type Attachment containing an adaptive card
};
Using the Summary property works on Desktop (i.e. it replaces the toast popup with the default “[Bot Name] sent you a card” message with the Summary content) but not on mobile. If it worked on mobile, this'd be just what I need.
Using the Text property on the Activity object is the closest thing to a workaround but it sends a second notification in addition to the first.
Is this possible to do? I'd appreciate any help with this, thanks!
I am well familiar with the concept of stacked notification .
The mobile doesn't show non-summary notifications if there is a corresponding summary notification. But if there is no summary notification, non-summary notifications are displayed
I am listening to every notification posted by NotificationListenerService introduced in Kitkat. I intercept and display every notification text as they arrive.
Problem is when stacked notifications arrive, I get callbacks for both groupSummary and non-summary notifications. If I have to decide if a non-summary should be displayed, I have to check every other notification for a summary.
How do I replicate the behaviour of mobile without going through the list of all present notifications repeatedly, that is, in less than O(n^2) complexity? Or does Android source code also do it the same complex way?
I devised a method myself with complexity < O(n^2). Guess I didn't think about using better data structures. Here's the function . Feel free to point out mistakes if any.
private ArrayList<StatusBarNotification> cleanseNonSummary(ArrayList<StatusBarNotification> notifications) throws Exception{
Set<String> groupSet = new HashSet<>();
//first run : add all summary notification keys to unique set
for(StatusBarNotification sbn : notifications){
if(NotificationCompat.isGroupSummary(sbn.getNotification()))
groupSet.add(NotificationCompat.getGroup(sbn.getNotification()));
}
//second run : remove all non summary notifications whose key matches with set elements
for(int i=0; i<notifications.size(); i++) {
StatusBarNotification sbn = notifications.get(i);
if (!NotificationCompat.isGroupSummary(sbn.getNotification())) {
String groupId = NotificationCompat.getGroup(sbn.getNotification());
if (groupId != null && groupSet.contains(groupId))
notifications.remove(i--);
//decrement counter if an element is removed
}
}
return notifications;
}
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
I am sending an SMS to multiple numbers. When I choose only one, it works, and the message is being sent immediately. But when I use multiple (3 for example) SMS Intent is opened, but after I click Send button I see "Sending..." message all the time, SMS is not sent.
Intent sms = new Intent(Intent.ACTION_VIEW);
sms.setType("vnd.android-dir/mms-sms");
sms.putExtra("address", getSMSNumbers);
sms.putExtra("sms_body", "Help!!!");
startActivity(sms);
And getSMSNumbers String looks like this: 512991220;505202222;606123456.
What is wrong? Why isn't the message being sent and it's "sending" all the time?
Also I see when I have more than 1 number, it's being converted to MMS - why?
Maybe you could loop through the numbers and send messages one by one, like this
android.telephony.SmsManager.getDefault().
sendTextMessage("00112233", null, "Message body text", null, null);
I managed to do this - needed to turn off "Group Messaging" in AVD Settings. App was trying to send MMS and that's why it wasn't being sent.