Android Notification Listener duplicate hell - java

Need a little help - this is doing my head in!
I'm the Dev of ReadItToMe, an app that reads out your messages and lets you reply by voice.
The issue I've been facing and hacking around the last year has been duplicate notifications. I'll use WhatsApp as an example.
If I get a single WhatsApp notification, happy days, it's one notification. If I get another from a different chat.. I get one group notification, the previous unread chat notification with a new post time so you can't check if it's from the past and then the new notification.
Solutions I've tried:
Keeping a list of notifications read by creating a hash code of notifications using their title, message and package name when they arrive and then removing them from the list when their dismissed (onNotificationRemoved).. But whatdya know! In the above scenario all notifications are removed and then resent so this solution is useless.
Comparing timestamps, which doesn't work as the duplicate notifications have new timestamps
Comparing keys/ids, these never change for a chat so they're not unique to a single notification of a message
Many others I've forgotten
Checking if the notification is a group to at least ignore that, doesn't work because the first notification is always a group. It's only when there's multiple notifications that the "group" is actually a group with content like "2 new messages".
Does anyone know of any way or can think of any creative solution to accurately determine (80% accuracy would do) which notifications are genuinely new and which have been previously posted?
Or is this just fundamentally broken on Android...

I had the same problem, I fixed it on this way, you can to use the "when" attribute combined with the saved-way using when:
public void onNotificationPosted(StatusBarNotification sbn) {
if (System.currentTimeMillis() - sbn.getNotification().when > 3000 ||
isInArray(sbn.getNotification().when)) {
//when != sbn.getPostTime()
//If notification is 3 seconds old, it is discarted
return;
} else {
//Else, push to the array of notifications "when-id" to compare next time
goNextPosition(sbn.getNotification().when);
}
... next part of code
}
long when
A timestamp related to this notification, in milliseconds since the epoch. Default value: Now. Choose a timestamp that will be most relevant to the user. For most finite events, this corresponds to the time the event happened (or will happen, in the case of events that have yet to occur but about which the user is being informed). Indefinite events should be timestamped according to when the activity began.
https://developer.android.com/reference/android/app/Notification.html#when
If someone has this problem, feel free to ask me, I'm sorry, I'm spanish native.

I have the same problem, duplicate push notification try this :
first : remove all duplicate application from your device, in my case i had 3 same application.
second : reinstall that application but try to change the package name, it's work for me. good luck

Related

Once I posted a notification message, is there a way to remove it or replace it?

I am working on a mobile app for Android. The app generates quite a lot of notifications, but they all have expiration tag on them. So, say, I don't want to display what was 2 hours ago if something, about the same subject, happened 1 hour ago.
Is there a way I can either
replace a posted message
or delete it in favor of a newer one?
You can use the Notifications Overview guide to see examples for various common notification use cases. The Notification updates and groups section has a couple of approaches that are relevant to your problem.
The simplest solution is to use the same ID in order to update the notification. As noted in the docs (quoted below), this will meet both your requirements.
To update this notification after you've issued it, call NotificationManagerCompat.notify() again, passing it a notification with the same ID you used previously. If the previous notification has been dismissed, a new notification is created instead.
You can optionally call setOnlyAlertOnce() so your notification interupts the user (with sound, vibration, or visual clues) only the first time the notification appears and not for later updates.

Adding new notification without deleting the old notification for my app in android studio

I am creating a TODO list app which accepts Notification time and date. Whenever i add a new scheduled notification my old notification get cancelled and the notification which i entered last alone works.
For example:
if i set a notification on 1,2,3 minute respectively i only get the final notification that is only one notification after 3 minutes.
Can someone explain how to clear this problem. If someone can pls share the code of broadcast and the defining and calling part of notification
I'm assuming, each notification replaces the previous one rather than generating its' own id. Fix that and you'll be on your way.
This happens because your notifications have the same id.
When you call NotificationManagerCompat.notify(id,notification),
use a different id for different notifications.

How to set timer, even if App is closed?

I habe a app for event informations an app which sends pushmessages. Now I want to give the user the option to disable the messages for a specific time (one hour, one day ...). I know how to dissable it compleatly, but how I can set it like I want (one hour...) even if the app is closed?
Well, the simple solution would be to check if there is a time restriction is present in the same place where you notify user for event information. If there is a time restriction dont show the notification else show it

How to send a broadcast message with SmackAPI?

We're developing special devices that uses XMPP to talk to each other. A new API i am developing now, should talk to these devices too. The problem i am facing - all these devices are building little groups - for each customer we have... so we will have a chat-room for each group of devices, or, for each of our customer with his bunch of devices ;)
But now.. the api should be able to talk to every device that is connected. I don't need a real broadcast-mechanism - in the end, i will send a message only to one specific device..
But i don't want to login to each chat-room either - running a product with over 40k customers and much more devices, will end in a funny api, that is opening over 40k chat-rooms at startup... even if don't tried this yet, i can't imagine that an app like this will run well... even though we can have millions of customers in a few years.. i don't like solutions that will grow linear with the amount of customers, if you know what i mean :/
Now, basically i'm thinking of a solution, where i just can use the basic XMPPConnection to do my stuff.
MyCustomMessage msg = new MyCustomMessage();
msg.setTo("*"); // don't know what to address, i want to send it to "all"
msg.setFrom("ThatAPI"); // just a string telling that is sent from my java api ;)
msg.setEvent(event); // stuff that is coming through the parameters of the method where this code is inside
msg.setCustomStanza(data); // same here
connection.sendPacket(msg); // my try to send it to all till now..
Somewhere in the Ignite Realtime Forums i have read of one guy who "solved" it, but everything he says is "it's working now, i push my message through the sendPacket of Connection"... ok nice, my attempt of this seems not to work :(
Any better ideas/real implementations how this will work fine?
Basically i start to think that XMPP will not be the best technology to achieve something like this at all - i wish i could have a real/basic socket-implementation where something like this would be piece of cake.. But i can't choose - the third-party-system has implemented XMPP already... not enough time to change all of this... Just if you're wondering why we try this on XMPP..
You seem to have some conflicting requirements in that you want to send to all devices now, but only 1 specific device later. Are both models required at the same time, or do you plan on switching? How either is done would be different solutions.
As for your current approach, I think pubsub would make more sense than your chatroom approach, as that is oriented to generic message passing to subscribers.
You could set up a pubsub node per customer to send messages to all
of their devices.
As for a broadcast to all, you can make all devices
subscribe to a single pubsub node.
Thus you control broadcast and group messages by sending to the appropriate pubsub node.
For sending to a specific device, that is just a sendPacket to the specific entity, nothing really special there.

Since when is the phone charging/discharging

I wanted to learn more about the Android Services / Broadcasts, so I started a simple project, to create a battery monitoring app. It turned out pretty good, I'm using it for a few days now, but I want to add a new function: to show since when is the phone charging/discharging.
First I thought that I would create two static fields in my BoradcastReciever extension class, where I get and publish the data about the battery, one for the actual state (charging/discharging), and one for the time, when the change in state happened. This way, I could just subtract from the current time the last change, and know exactly since when is the phone charging/discharging.
But there is a problem with this solution: It won't show the correct data at first, when a user starts the app. I wouldn't make a big deal of it, but I saw that Android tracks this data somewhere, because inside my phone settings I found this information, so why take the hard way.
So my question is: is there an easy way to get from the Android system the date/time (no matter what format) of the last charging state change?
I looked at the BatteryManager reference but there are no constants named after what I seek, and which I could use, to get the information from the Intent of my receiver.
The Android OS tracks the connect/disconnect of a power source, but does not make this data accessible to apps. You have to record this all yourself, using intent filters.
The two intent filters to use are android.intent.action.ACTION_POWER_CONNECTED and android.intent.action.ACTION_POWER_DISCONNECTED; with these, you can monitor when the power source is connected and disconnected.
You can find information about this process explained incredibly clearly here. Another blog describing the process can be found here.

Categories

Resources