I am developing an android appliation that marks SMS messages as read when it is received. I do that using this code:
if(readMessages.contains(id)){
ContentValues values=new ContentValues();
values.put("read", 1);
values.put("seen", 1);
getContentResolver().update(Uri.parse("content://sms/inbox"),
values, "_id="+id, null);
Log.i("read message","id:"+id);
if(readThread.contains(trdid)){
ContentValues values1=new ContentValues();
values1.put("read", 1);
values1.put("seen", 1);
getContentResolver().update(Uri.parse("content://sms/inbox"),
values, "_id="+trdid, null);
Log.i("read thread","id:"+trdid);
}
}
As you can see, I've marked the thread and message id's as READ, and the message is indeed marked as read.
My question however is, why doesnt the icon on the messaging app (the red bubble showing the number of new messages) dissappear after I execute the code? Is it possible to do that?
Thank you! :)
why doesnt the icon on the messaging app (the red bubble showing the number of new messages) dissappear after I execute the code? Is it possible to do that?
You cannot control the notifications from an app that is not your own. Additionally, AOSP messaging does not have red bubble notifications, so this is some custom app.
Regardless of that though, you can't control the notification status of a third party app. It should be the responsibility of the SMS client to update its UI and notifications when a change in the SMS database takes place.
Related
This push notification is used to notify when a new chat message is available. I want to do like Instagram, that is to say, the message into this notification is updated at each new income message received, until the user open the chat.
So, how can I retrieve the message for this specific notification? Is it possible, or I have to save (in cache) the latest messages... ?
Thank you very much
I suggest saving messages in the database, and then in the function that sends notifications, get messages (probably recent unread ones - I don't know what your logic looks like in this case).
If there are more than one, use InboxStyle():
val otherMessagesToShow = getMostRecentUnreadMessages()
if(otherMessagesToShow.size > 1) {
val style = NotificationCompat.InboxStyle()
otherMessagesToShow.forEach { style.addLine(it) }
builder.setStyle(style)
builder.setNumber(otherMessagesToShow.size)
}
Finally, update the existing one (passing the same notificationID):
notificationManager.notify(notificationId, builder.build())
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 I use WhatsApp or Telegram and receive N messages, the notification on Android will show "N new messages" (and it can be expanded).
Telegram shows notification with contentText "7 new messages" on Android. I have achieved this successfully.
However, on my Pebble Time, the last notification is the string of the last message, not "7 new messages".
On Pebble, it shows the 7th (last) message (the censored part is phone number). This is what I want.
I am trying to develop similar feature but have not succeeded. The notification on my Android displays correctly ("N new messages") but on Pebble Time, it's identical (also "N new messages", I want it to be the Nth message).
My app on Pebble. This is NOT what I want.
I have tried to call .notify twice (one contains "N new messages" and the other contains last message) and immediately .cancel the latter but Pebble Time only shows the first one.
If I don't call .cancel on the second notification, my Pebble will show what I want BUT there will be 2 notifications on both Android and Pebble (which I don't want).
How do I achieve similar feature like WhatsApp and Telegram?
Update:
This is the snippet I use (I have used different notification ID)
NotificationCompat.Builder nb = new NotificationCompat.Builder();
...
nb.setContentText("2 new messages");
notificationManager.notify(1, nb.build());
nb.setContentText("B");
notificationManager.notify(2, nb.build());
notificationManager.cancel(2);
Because you use same id for two notification item. you can try with different id for each show notification item.
From Android docs: https://developer.android.com/reference/android/app/NotificationManager.html#notify(int, android.app.Notification)
id: An identifier for this notification unique within your
application.
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 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.