Hello i make one example which show count of notification which user got, with help of NotificationListenerService what i actually implemented yet...
public void onNotificationPosted(StatusBarNotification sbn) {
final String pkgnm = sbn.getPackageName();
int ID = sbn.getId();
}
but my problem is some application notify twice and also there time milisec is different and key also different.
So how can i filter the notification only once.
Thanks in Advance
Related
when my app in foreground then the notification url link open automatically without user's any contribution.
Now,I want to keep notification in notification bar and when user press the notification that will open the url link.
One more thing I have to mention that,when my app in background it works fine.Notification arrive in notification bar.
I want same thing to do for foreground.
please help,
Thanks in advance.
my FirebaseMessagingServices
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getData().size()>0){
String url = remoteMessage.getData().get("url");
Intent intent = new Intent("com.bellecarib_FCM-MESSAGE");
intent.putExtra("url",url);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.sendBroadcast(intent);
}
}
here is my MainActivity code
LocalBroadcastManager.getInstance(this).registerReceiver(mHandler, new IntentFilter("com.bellecarib_FCM-MESSAGE"));
if(getIntent().getExtras() != null){
for(String key: getIntent().getExtras().keySet()){
if(key.equals(("url"))){
mwebView.loadUrl(getIntent().getExtras().getString(key));
Toast.makeText(getApplicationContext(),"Opening The Notification Page",Toast.LENGTH_LONG).show();
}
}
}
The behaviour you are seeing , is when you send notification payload from FCM. If you want a customised behaviour, then send only a data payload and when you receive the message, throw up a notification manually. This means more work for your app (at the cost of customisation). Also data payloads are delivered with a normal priority which means if your device is in idle mode, then FCM Data payload messages are not delivered immediately and deferred till the next maintenance window (Read upon Android Doze mode and App Standby).
Hope this is clear
I want to remove all cancelable notifications from status bar. I know how to remove my app notifications I have seen this post before, butI want to remove other apps notifications.
There is a "Clear" button in notifications in all android versions which clears all notifications with this flag: Notification.FLAG_AUTO_CANCEL
That means it's possible to cancel all notifications. But I haven't found any document about how to do that. Is there anybody guide me how to do that?
Thanks
Starting with API Level 18 you can cancel Notifications posted by other apps different than your own using NotificationListenerService, the method changed a little in Lollipop, here is the way to remove notifications covering also Lillipop API.
First inside the onNotificationPosted method you store all the StatusBarNotification objects.
Then you should maintain an updated reference to those objects, removing them if the notification is somehow dismissed in onNotificationRemoved method.
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationService extends NotificationListenerService {
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
// Store each StatusBarNotification object
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
// Delete removed StatusBarNotification objects
}
}
Finally you can remove any notification using cancelNotification method.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
else {
cancelNotification(sbn.getKey());
}
To remove all notifications:
Kotlin
val NM = getSystemService(AppCompatActivity.NOTIFICATION_SERVICE) as NotificationManager
NM.cancelAll()
Java
NotificationManager NM = getSystemService(AppCompatActivity.NOTIFICATION_SERVICE)
NM.cancelAll()
I just implemented GCM and notifications in my Android app, coming from an Apache/PHP-based webserver.
The notifications are already working, but I'm stuck at stacking the notifications, as described here.
What I'm trying to do
I have two types of notifications in my app, using data coming from the GCM Service:
Type 1 (Messages):
[data] => Array
(
[t] => 1
[other data...]
)
Type 2 (News):
[data] => Array
(
[t] => 2
[other data...]
)
These 2 types are completely different notifications, and I would like to stack both of them separate from each other, but I can't get this to work.
I would like to stack them like this, as soon as there are multiple notifications:
Default View
Expanded View
What I tried
2 Notification IDs and Atomic Integer
I tried to use 2 different notification IDs, so that notifications with the same type get overidden.
if (msg.get("t").toString().equals("1")) {
notificationNumber = messageCounter.incrementAndGet();
} else {
notificationNumber = newsCounter.incrementAndGet();
}
[...]
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setNumber(notificationNumber);
If 2 messages are sent at the same time, everything works fine and the counter shows 2. But if there is a short delay between two notifications, the counter switches to 1.
Unique Notification IDs
I also tried to use unique IDs generated with
Date now = new Date();
Notification_id = now.getTime();
so that there isn't no stacking or overriding at all.
Question
How can I solve my problem? Can I access the content of the previously sent notifications, so that I can show every message in one line, like in the expanded view of Gmail? How can I check which / how many notifications are currently displayed?
Long question, thank you very much!
I finally found the solution and ended up using atomic integers, but in a seperated class:
import java.util.concurrent.atomic.AtomicInteger;
public class Global {
public static AtomicInteger Counter1 = new AtomicInteger();
public static AtomicInteger Counter2 = new AtomicInteger();
}
To reset the counter after the application opening, i put this in my MainActivity (called in onCreate() and onResume():
private void clearNotifications(){
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
Global.Counter1.set(0);
Global.Counter2.set(0);
}
When I create the notification, I check the counter:
Counter1 = Global.Counter1.incrementAndGet();
ContentText = (Counter1 < 2) ? /* Single notification */ : /* Stacking */;
I've had a problem with NotificationManager in my app for the past couple of days and I don't seem to be getting closer to solving it.
I have a very simple service which does not do anything at the moment. It is just supposed to display notification:
public class UpdateService extends Service {
private static final String TAG = "UpdateService";
private static int NOTIFICATION_ID = 1;
private UpdateServiceBinder binder = new UpdateServiceBinder();
#Override
public void onCreate() {
Log.i(TAG, "Service created");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Service started");
sendNotification(100);
return Service.START_NOT_STICKY;
}
private void sendNotification(int updatedItems) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.icon)
.setContentTitle("Sync")
.setContentText("Updated " + updatedItems);
Intent resultIntent = new Intent(getBaseContext(), MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendindIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendindIntent);
NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
}
#Override
public IBinder onBind(Intent intent) {
return binder;
}
#Override
public boolean onUnbind(Intent intent) {
return true;
}
public class UpdateServiceBinder extends Binder {
public UpdateService getService() {
return UpdateService.this;
}
}
}
Unfortunately, when the service is started and the notification is supposed to be displayed nothing happens. The service is definitely started according to log messages. At the same time there is warning from NotificationManager:
11-30 23:24:34.620: I/UpdateService(28356): Service created
11-30 23:24:34.800: I/UpdateService(28356): Service started
11-30 23:24:34.808: W/NotificationManager(28356): notify: id
corrupted: sent 1, got back 0
I tried using different numbers than 1 but it did not help. I am not sure what to make out of it. The code I'm using comes from the Android documentation on service in here and notifications in here. When I isolate the code in separate clean app which is setup in similar way to the one I'm working on notification is displayed correctly. Has anybody had this problem before or has any ideas?
Any help appreciated.
Thanks!
I had this problem then I remember that I had disabled the "Show Notification" from my "App info". I enabled it and notifications are back again.
Go to Settings > Apps (Application Manager).
Tap the app you want to stop.
Tap to uncheck the box for Show notifications.
FYI it might differ from one android device to the other, however they are all more or less the same.
Startin from API 25 (Nougat), Android puts rate limiting in place to block apps from DDOSing the notification shade. The commits to AOSP introducing this change can be seen here.
I was seeing these logs on my side because I was trying to update a progress notification inside a loop:
Observable.range(0, 100)
.subscribe(progress -> updateProgressNotification(progress));
Adding an delay of 200ms between each emission fixes the problem for me:
Observable.range(0, 100)
.zipWith(Observable.interval(200, TimeUnit.MILLISECONDS), (progress, delay) -> progress)
.subscribe(progress -> updateProgressNotification(progress));
You can read more about this problem on my blog.
I found a solution to this problem. I had to change the package name from com.gttn.sample to com.gttn.sample2. Not really sure why I had to do it but the notifications show properly and the warning disappeared.
When you install and reinstall the app many times something
unchecked the "Show Notifications" in Settings > ApplicationManager > "Your app".
Just check again "Show Notifications" and be happy!
I'm not sure why you're using getBaseContext(), but I suggest you use getApplicationContext() instead. Also, I'm not sure why you're binding to the Service.
just looked into this problem myself. without additional lines from your logfile (should be within 5 lines after) there might be a message similar to "E/NotificationService( 287): Suppressing notification from package by user request." This is a new setting in ICS (maybe JB) to disable notifications on a per-app basis. Go to Settings --> Apps --> and click the checkbox "show notifications" near the top. for obvious reasons, changing the package name would circumvent this setting very effectively.
I have written code which download the song and update the progress of the download as the progress in the notification. If u see in the line 6 of code ,If that condition is removed then notification is updated very fast as the while loop reads the bytes from the input stream now this causes repeated updates on the notification and this is recognised as the problem starting from noughat verions which keeps the rate limit on the notification update.Hope you got some idea about it.
int previousPercentage = 0;
int currentPercentage = 0;
while ((bytesRead = inputStream.read(bytes)) != -1) {
totalbytes = totalbytes + bytesRead;
currentPercentage = (totalbytes * 100) / length_of_file;
if (previousPercentage != currentPercentage) {// line : 6
updateProgressNotification(builder, notificationId, currentPercentage);
}
previousPercentage = currentPercentage;
fileOutputStream.write(bytes, 0, bytesRead);
}
Android: I am trying to cancel a notification from the notification bar after a package being installed.
What I am doing is the following:
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
Uri data = intent.getData();
//some code goes here
//get the id of the notification to cancel in some way
notificationhelper._completeNotificationManager.cancel(id);
}
}
}
where
public class notificationhelper {
public static NotificationManager _completeNotificationManager = null;
public void complete() {
if (_completeNotificationManager == null)
_completeNotificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.notification,
_context.getString(R.string.notification),
System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_NO_CLEAR;
_completeNotificationManager.notify(TEXT, id, notification);
}
}
But the notificationhelper._completeNotificationManager.cancel(id) does not work. I tried to use notificationhelper._completeNotificationManager.cancelAll(); and it works. What I am doing wrong?
In my experience, you can't cancel all notifications with a particular ID, regardless of tag.
That is, if you create two notifications like so:
notificationManager.notify(TAG_ONE, SAME_ID, notification_one);
notificationManager.notify(TAG_TWO, SAME_ID, notification_two);
Then, notificationManager.cancel(SAME_ID) won't cancel either of them! I suspect that this is because the "tag" field, if unspecified in notify() and cancel(), defaults to null, which you have to cancel explicitly.
So, to cancel these two notifications, you have to call:
notificationManager.cancel(TAG_ONE, SAME_ID);
notificationManager.cancel(TAG_TWO, SAME_ID);
In your case, you're supplying "TEXT" as the tag but cancelling just using the id, which defaults to using tag=null.
So, either don't provide TEXT as your tag:
_completeNotificationManager.notify(id, notification);
Or, if you need separate notifications and don't want them to clobber each other, keep track of the active tags:
_completeNotificationManager.notify(TEXT, id, notification);
collectionOfActiveTags.add(TEXT);
...
for (String activeTag : collectionOfActiveTags)
notificationhelper._completeNotificationManager.cancel(activeTag, id);
I wish that what you're trying to do was supported, as it seems that it should be.
Well this is probably irrelevant at this point, but it should be posted here so that people like me dealing with the same problem might find the solution.
If NotificationManager.cancel() isn't working, try changing the ID for the notification.
notificationManager.notify(NOTIFICATION_ID, notification);
When I changed NOTIFICATION_ID from 1 to [RANDOM_NUMBER], it magically started working. I assume that 1 is somehow reserved, although there is no note in any documentation...
An of course make sure you use the same NOTIFICATION_ID to cancel:
notificationManager.cancel(NOTIFICATION_ID);
My notifications were not getting removed because my service was Foreground Service and NOT a regular service started by StartService.
If your service is foreground, call stopForeground(true) instead of stopself(). So now my code looks like this:
NotificationManagerCompat.from(this).cancel(NotificationHelper.PLAYER_NOTIFICATION_ID);
stopForeground(true);
and it worked, notification was removed.
I was facing the same issue recently. I have managed to solve it.
So from what i understood.
use the id which is basically a random number to notify and send this same id to the piece of code (receiver/activity...) where you want to cancel it.
When using tags, it seems to not work for me as I was giving one tag to all notifications but with unique id. It worked only on the first tag so I completely avoided using tags. If you want to use tags, issue unique tags along with unique id and use them both while cancelling.
So final answer... what I used and what works for me:
STEP 1:
int notif_id = (int)(System.currentTimeMillis()%10000);
STEP2: add this id inside the action intent (I am launching an activity where the notification gets cancelled on the action click):
Intent notificationSettingsIntent = new Intent(context.getApplicationContext(), NotificationSettingsActivity.class);
notificationSettingsIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationSettingsIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
notificationSettingsIntent.putExtra("fromNotification",true);
notificationSettingsIntent.putExtra("notif_id",notif_id);
PendingIntent notificationSettingsActivityPendingIntent = PendingIntent.getActivity(context,notif_id,notificationSettingsIntent,PendingIntent.FLAG_ONE_SHOT);
STEP 3: notify using the id in the step 1 but with no tags
NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(context.getApplicationContext());
notificationCompat.notify(notif_id,notificationBuilder.build());
Now in the Activity which gets opened by my action click, I cancel the notification as:
NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(context.getApplicationContext());
notificationCompat.cancel(getIntent().getIntExtra("notif_id"));
Works every time now.
Sorry for late joining!
But following worked fine for me.
NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(context.getApplicationContext());
mNotificationManager.cancel("<TAG>",<Notificatoin-id>);
Following worked for me:
final NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(context.getApplicationContext());
mNotificationManager.cancel(<Notificatoin-id>);
Since there is no accepted answer, I am posting another one with same scenario I faced
private fun stopForegroundService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
stopForeground(STOP_FOREGROUND_DETACH)
}else if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
stopForeground(true)
}
notificationManager.cancel(NOTIFICATION_ID)
}
Point to note is first you need to set stopForeground(false) then call notificationManager.cancel(NOTIFICATION_ID)
If you change the order, it won't work