Clicking on a generated notification cancels an existing AlertDialog - android

I'm building an Android app that handles incoming events.
When I receive 1 such event, if my app is in the foreground, I immediately generate an AlertDialog that requires the user's attention.
However, when the app is in the background, the user does not see this, even though the AlertDialog has already been generated. If I manually go back to my app, I see the AlertDialog and I'm able to click OK or cancel.
I tried generating a notification dialog that appears when my app is in the background. This works, and I put the PendingIntent as MainActivity. When I tap on the notification, it brings me back to my app. However, the AlertDialog isn't there anymore.
Comparing the logcats of tapping on the notification vs manually going back to my app, I notice several differences.
When I manually go back to my app, I see MainActivity is resumed immediately. However, when I tap on the notification, it first goes back to the constructor of the class that generated the notification, a few other classes/fragments, before finally resuming MainActivity.
Does anyone know why this might be the case?
Thanks.

The default behaviour of a foreground notification is to re-launch the activity. Try setting your intent flag for the PendingIntent to use this:
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
And that will then have the same behaviour as if you navigated to the activity through your app.
From docs:
If set, the activity will not be launched if it is already running at the top of the history stack.

it seems that,you are is creating new instance of app when resuming from notification
so,add this in manifest,it might help:
android:launchMode="singleInstance"
in other case it resumes hence dialog going to be persisted.

Related

Can multiple full-screen Intent activities launched by alarm notifications co-exist (on top of each other)?

I have an alarm/reminder app that uses .setFullScreenIntent in its notification builder. As a result, when the phone is locked/screen off and the notification triggers, the full screen activity shows up and can wake up the screen. It works well.
But here's the problem: say the user missed this full screen notification entirely, and never cleared it (by clicking the dismiss button I put on this screen). A while later, another notification comes in. It will override the previous one entirely.
Now if the user comes back, only the latest full screen notification is shown. After dismissing it (which calls finishActivity in the code), the previous ones are not shown beneath it, even though they are present in the notification area.
The activity is already in standard mode, not singleTop, so multiple instances should have been allowed. I also made sure to set different requestCode on all of the underlying intents.
Ideally, I want multiple such full-screen notifications to stack on top of another (last in, first out) so the user can clear them one by one without missing any of them.
Is it an Android limitation that only one instance of full-screen notification is allowed per app at a time?
In my case, I also had launchMode set to standard. Took me several hours but got it to work by removing setContentIntent() in my notificationBuilder while retaining setFullScreenIntent().

Opening the App from Recents opening the old activity in Android?

I have 4 Activities: Launcher, MainActivity(SingleTask), NotificationActivity, ExampleActivity.
When the user clicks on the notification, NotificationActivity opens then ExampleActivity and finishes the NotificationActivity.
When I press back MainActivity opens. Now if I press back, the application gets closed. If the user then opens the app from the recent Tasks, NotificationActivity opens then ExampleActivivty. But if the user opens the app from the icon, Launcher Activity opens then MainActivity.
How to handle this situation?
Exactly this problem is covered in my answer to this question:
Remove data from notification intent
When you select your app from the list of recent tasks, Android remembers how the app was originally launched (in your case, via Notification) and starts the app again the same way. This can be considered a bug or a a feature, depending on what you expect to happen.
I've got a few suggestions in my answer that you can use to deal with it.

Open Gmail PendingIntent in background?

I am trying to open a PendingIntent that opens the Gmail App in the background through a notification.
The steps are as follows:
Show notification with action button
On click of action button (which has a PendingIntent), I want start the PendingIntent (which launches the Gmail app) but in the background.
Dismiss Notification
The notification remains the only thing that the user has seen (i.e. the UI has not changed but only the notification has itself been dismissed after clicking the action button).
It may be "hack-y" but I could also quickly open and then "minimize" the gmail app?
Any ideas?
OK, I really don't understand what you can achieve from such behavior.
you definitely could not provide any meaningful extra data on the intent (unless you are the developer of gmail, which I don't believe is the case..).
actually, if you provides intent to launch explicitly Gmail app (by specifying the package name) - your code would break if Gmail app package name would change.
now to your question:
it is possible to launch activity without bringing it to foreground:
all you have to do is add to the intent been held by the pending intent to flags: FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_MULTIPLE_TASK
FLAG_ACTIVITY_MULTIPLE_TASK flag is not recommended for use unless your application is the launcher.
gmail main activity would create in background new task, and that's it.
note that the gmail activity would not go threw the onResume and onStart callbacks (because it's not on foreground..) , so if any meaningful code happens from this callbacks - they won't be executed anyway..
UPDATE
now that I know that your purpose is to mark an emails as "read", I can tell you that any attempts to launch gmail app want do to you any good. as I mentioned, you can't pass extra meaningful data to gamil launching intent. there is simply no such API's, and launching main activity simply won't do anything that would help you.
instead, I'll suggest you to use Gmail API's for control user's inbox

How to use PendingIntent for two different activities?

I am using PendingIntent to take user from push notifications to Activity B i,e when user clicks on notification it will go to screen B.
I want it to be screen B only when application is loaded and running, else it should go to screen A. I have implemented this as well.
Now, there is scenario, wherein I have received the notifications, hence the intent is set to Screen B, but I haven't clicked it. If I kill the application process using DDMS or if the android kills the application before I click on notification and then I click it, it tries to load activity B which is not what I want in this case. I would like to load activity A in this case which is first sceen of the app.
Please, suggest what to do !
It sounds like you're deciding whether to launch activity A or B at the time you post the notification, which, as you've discovered, will not work: once you submit the notification to the Notification Manager, you can't make changes to it.
The way to do this is to always launch the same activity, but have that activity potentially redirect the user to a different activity based on whatever criteria you want. For example, you could launch activity B, which then bounces the user immediately to A if the app is not already in the foreground. (You could also do this with a third activity whose job is simply to route the user around, or with a service, or with a broadcast.)
Now the decision about where to send the user is made at the time the user taps on the notification, rather than at the time the notification is posted.

Notification to not reload app in android

I am currently developing an android app which uses the android web browser and notifications.
What I want to be able to do is the user clicks on an item which loads the android web browser and makes a notification in the notification bar as well.
The user should be on the browser when they go the notification, so when the user is on the browser and they click on the notification I want the notification to perform its task but not redisplay the app activity instead just return to where the user was on the browser.
I've tried setting the different flags on the activity but none of them seem to make any difference.
Thanks for any help you can provide.
According to the following quote from the Android documentation, it sounds like there isn't a way to do some kind of background task without starting an activity when a user clicks a notification. Although you may want to look into sending an Intent to a Service. I've have not tried that before so I can't say whether it works or not.
A notification always starts an
activity as a new task (that is, it
puts FLAG_ACTIVITY_NEW_TASK in the
intent it passes to startActivity())
The documentation also suggests you could have a dedicated activity that could perform the copy paste, and then the user could simply press back to get back to the browser:
For example, when the user receives a
Calendar notification, choosing that
notification starts a special activity
that displays a list of upcoming
calendar events — this view is
available only from the notification,
not through the Calendar's own user
interface. After viewing this upcoming
event, to ensure that the user
pressing the BACK key will return to
the activity the user was in when they
picked the notification, you would
make sure this dedicated activity does
not have the same task affinity as the
Calendar or any other activity. (You
do this by setting task affinity to
the empty string, which means it has
no affinity to anything.)
To get round this as I did not want to have a service running in the background to perform this simple task what I have done is called the method at the end of the activity that the notification calls.
I call moveTaskToBack(true); which places the task into the background. As if the user presses the home button.

Categories

Resources