I have an ongoing task and after it finishes, I want the notification to become cancelable.
For that, I create a new notification with notification.flags as zero, but the notification keeps being un-cancelable.
Apparently, FLAG_FOREGROUND_SERVICE prevented the notification from turning into cancelable, even after reseting the flags.
After removing FLAG_FOREGROUND_SERVICE and using only 0 or FLAG_ONGOING_EVENT, the notification could be made cancelable or un-cancelable - respectively.
Please take a look here : Android update notification
In short what you will do is this:
Create your notification first time and assign a notification ID to it.
Once your service is done executing create a new cancelable notification with the same id
fire that notification, it should make the previous notification cancelable .
Related
I have an Alarm App that have foreground service with a Heads-Up Notification and that notification have two actions where one send an intent to the Service and can open an activity depending on the app configuration.
The problem is that when i click on a action that sends the intent to the service the notification doesn't hide. This not seems to occur when the intent opens a Activity
I don't want a foreground service without a Notification, i just want it to hide it back to the Notification Drawer when the intent is sent to the service
Here is the code:
NotificationCompat.Builder(mAlarmApplication, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_alarm)
.setAutoCancel(false)
.setOngoing(true)
.setVibrate(LongArray(0))
.setContentTitle("Title")
.setContentText("Content")
.addAction(0, dismissActionText, dismissPendingIntent)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(alarmScreenPendingIntent)
.setFullScreenIntent(alarmScreenPendingIntent, true)
Here is the link of the app https://play.google.com/store/apps/details?id=com.garageapp.alarmchallenges.
The problem occurs when alarm start and my current solution is to update the old heads up notification with a new one that is not a heads up but the UX is not a good because on Android 8+ the notification new notification pops up aging
Seems like your Notification is bonded with your Service. If so, then you have to kill the notification in Service
Did you try?
public static void cancelNotification(Context ctx, int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(notifyId);
}
You are using .setOngoing(true) which should not be removed while service is working.
.setAutoCancel(true) will also not working with .setOngoing(true).
You have to use .setOngoing(false) to dismiss the notification.
If you or user remove your foreground notification your service will go to background, I think that best work is to not using heads up notification for foreground by not setting its priority to MAX
Use two notifications at same time one in drawer and another heads up:
-The first notification with priority DEFAULT for starting foreground ( auto cancel set to false and ongoing set to true) show this one with startForground()
-The Second notification (Heads up (Priority MAX) auto cancel set to true and on going set to false) for your actions show this with notifyManager.notify()
These two notifications must have different IDs
another solution:
If you want to use one heads up notification with actions for foreground service you may do this:
use a heads up notification with your action buttons for foreground service when the user clicks actions this action must call the foreground service and then the foreground service could call startForeground (with same id) with a new notification with priority set to default, if your notification could not be updated you may need to call stopForeground(true) or notificationManager.cancel(id) first before calling startForeground with new notification. both of these two notifications should has on going set to true and auto cancel set to false
In my opinion the first solution is better than the second because the notification may not update in second solution.
As the documentation says :
A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of ...
android system does not allow you to have a foreground service without notification or a hidden notification. and that's because of user awareness of what is happening in his/her system.
also killing the notification will stop your foreground service.
so you never can have both of the options (foreground service and hidden notification)
a not clear solution for your problem:
when you call action that sends the intent to the service, do this with a mediator activity i mean first open an activity and in the activity send intent to the service.
I hope this solve your problem as you told :
The problem is that when i click on a action that sends the intent to the service the notification doesn't hide. This not seems to occur when the intent opens a Activity
I have integrated pushwoosh in android application (not in titanium)..
I am successfully getting notification also..
My issue is,
Text is displayed on notification area which I passed from woosh..
BUT NOT ABLE to extract it from intent
intent.getExtras().getString(PushManager.PUSH_RECEIVE_EVENT)
Also my notification does not remain on notification area..
it only displayed once and disappear, I am not able to click on that
because it is not staying there..as like other notification..!!??
Please can anybody suggest any thing..?
For notification to remain on the notification bar there is falg to set in Android Notification.
Notification not= new Notification(R.drawable.icon,title+" "+context.getString(R.string.Start_in),System.currentTimeMillis());
not.flags=Notification.FLAG_NO_CLEAR;
For Android Notification there is One falg to Clear the notification or not. So I think there will some flag to Not clear the Notification when it arrives.
So Try to find out the flag for not clearing the Notification.
As you are setting flag for sound
pushManager.setSoundNotificationType(SoundType.ALWAYS);
I would like to animate the icon of the app without having to cancel the notification and create a new one (because in this way the icon doesn't stay in the same position of the notification bar but could move to first place if there are other notification running).
I'm able to get this with normal notification, but I would like to get the same behaviour when I use startForeground in my service. This method launches a new notification which can't be removed unless you remove service from foreground using stopForeground.
Is this possible to do? How?
Use the same notification ID for the startForeground method and the Notification object.
How to create a static status bar notification? Like in skype app or others...
It must be visible while app is running, even if it runs in background.
Create a notification and include FLAG_ONGOING_EVENT in the set of flags. Note that when your application is finished, you should cancel the notification.
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
What's the best way to clear the notification number when the user clicks on the notification? I say the best way, but really I haven't found ANY way. I'm launching a built in activity when the user clicks on the notification, not something I wrote so I can't clear it that way. I've got the notification manager's flag set to clear
NotificationManager notification
.
.
.
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number++;
nm.notify(1,notification);
But whatever I do the Notification.number keeps going up and never resets to 0.
You could use an intermediary activity which is part of your app and thus can reset your variable and then start the internal activity.
So the chain would be
Notification --starts--> Intermediary Activity --starts--> Built-in activity
I am using a combination of what #CommonsWare recommends and extending an application object as here How to update notification number .
Edit: Further testing shows that actually this is not working because the BroadcastReceiver is called on every notification and because it reset the number on every notification the number is never correct.