Prevent launched notification intent from taking over the recents window - android

I have a notification that notifies that a long operation has succeeded and offers the user to do something after that.
Leaving the notification aside and opening the app, makes the app apear in the recents - even when the back is pressed and the activity stack is left empty.
The problem is that when the user clicks on the notification, the notification intent is then registers in the recents - instead of the app's main activity. Clicking on the recents over and over again does the notification intent action instead of popping up the app.
How can I have the app stay in recents instead of the last notification intent?
Notes:
When the app's activity stack is not empty, everything works perfect!
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS is not acceptable because it removes the app altogether from the recents instead of just the notification intent.

Related

What's the difference between tapping notification when app is on background and terminated

When tapping the notification with message data through type 3 (open app), the message data are all get from onNewIntent method, is there any difference between when the app is on background and terminated?
If tapping the notification when app is on background, it will reset to the initial screen or keep the previous route path?
When you tap on the notification, it will go to the Intent your specified when creating the notification. There is no difference there. If the app was terminated, it will need to be restarted. In that case any data that was present in memory will have been lost, so anything you need to handle the notification will need to have been written to disk.
When the launch mode of an Activity is set to single task, onNewIntent callback will be invoked when a user clicked on the notification where setContentIntent(pendingIntent) has been set.
onCreate won't be called if the app is already running and is visible to the user but onNewIntent will be invoked. onCreate would be called if the app isn't already running and visible to the user. In onNewIntent, the intent passed into the pendingIntent would be delivered to onNewIntent.

How to trigger pop-up in app via notification click even when app is not running

I'm trying to implement a timer in my app. The timer runs in a foreground service, and it works as intended.
Upon clicking the the notification, it should a) open the Timer activity (which is the Main activity) and b) open a one-time pop up asking if you want to stop it. (not the full extent of the pop up, but I've simplified it for this example).
I am having difficulty with this functionality because of a few challenges/limitations. My two main methods is using RxJava and broadcast receivers via Pending intents.
If the app is open and in the foreground, I can use an observable via a broadcast intent and listen to it the Timer activity. when it receives a value, it shows the pop up. Simple.
If the app is closed or in the background, I've tried using a pending intent with a key-value extra such as ("should-show-pop-up",true). I then check in the onStart method if this extra exists and if true, show the pop up. However, now it shows the pop up every time I open the app. If the app goes in the background and then back to the foreground, the activity still holds that extra and it triggers the pop up.
So bottom line: how do I open my app and show a pop up if a user presses a notification? Even if the app is closed or in background
Best way to do this is to use a separate Activity for this not your main Activity. When you create the Intent to put into the Notification, add the following flags:
Intent.FLAG_ACTIVITY_NO_HISTORY
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
This will ensure that the Intent with the "extras" does not become the "default Intent to use when your app is launched from the HOME screen"

Keep my Activty open on notification click?

I have a strange scenario in my application where I want to keep my activity open even though if any notification is clicked. The scenario is I have splash activity that opens the Home Activity and then the OfferActivity(opened through home activity after getting a response). Now I want to remain on the offer activity even after users click on any notification received meanwhile. The problem is all my notification enter into the app through splash screen with intent data and then splash screen pass the data to the home activity which perform the specific action. Please help with your suggestions on the best way to handle it. At the moment I am clearing the notification tray when I opened the offer activity and block the notification display for the moment.

Deciding which activity to open on notification click

I'm currently trying to find out, how to specify the destination activity on click on an android notification - but not until actually clicking the notification.
I have the following use case: I have an app where the user has to log in before using it. So if the user receives a notification from my app while he is authenticated and using the app in foreground, a click on the notification should lead to lets say ExampleActivity. But if the app is in background and a notification is received, a click should redirect to LoginActivity, where the user has to authenticate first.
Since the destination activity is specified when creating and setting the Intent to the NotificationCompat.Builder, I have no later control over the destination activity.
So if the user is logged in and receives a notification, closes the app and clicks on the notification, he gets redirected to ExampleActivity, even though he would have to log in beforehand, which is a security flaw for my process.
I feel like, there should be a common approach on how to solve this kind of problem. Is there a possibility to check if the app is running during click on the notification and decide where to redirect the user to?

Android: If app loaded from notification, doesn't show in task manager

Not sure this is supposed to happen but to replicate:
Kill the app
Send a push notification via GCM to the app
Tap on the push notification (which loads the app)
The app then loads with the Activity set in the PendingIntent
Then tap the home button
Now bring up the task manager and notice the app is no longer there.
Has anyone seen this before, as it doesn't seem to happen for any other apps I've seen on the store?
So to answer my own question, the reason this was happening was because the Activity I had in my PendingIntent didn't have the LAUNCHER category associated with it.
So when the Activity was loaded, it didn't actually load the entire app but just the Activity on its own. I.e. the Activity was just loaded as a sheet "on top" of the UI.
So to fix, I just changed the PendingIntent to point to my MAIN activity, and when that was loaded, read the extras from the intent and then load the related Activity.

Categories

Resources