Deciding which activity to open on notification click - android

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?

Related

Cannot intent to specific page of apps from notification builder using FCM (android)

im beginner of using FCM to make notification in my android apps. I successed to make notification with FCM in my android apps but when i did not open my apps and there was a notification and i clicked it, it cannot intent to specific page should be, it just show the main page of my apps. But when i opened my apps, and then there was notification and i clicked it, it show the specific page. Even though, i have make my apps always running in backgroud. What is the problem? Can i make my apps intent to specific page should be although im not open my apps? Please help..
Thankyou...
Yor MainActivity is entry point of your app, so if your app not startup, click the notification will invoke it first, if you want to jump to spefic page, you should add some jump flag to your notification message body, so when the mainactivity startup, you can check the flag and jump to correct page.

Android - Handle local notification when app in foreground

My app receives FCM and fires local notifications, which open target activities. This works fine if the local notification is clicked when the app is not running.
But, if the local notification is clicked when my app is already running (foreground or background), I don't want to open the target activity but shows an AlertDialog first. So, the activities stack is not changed and the user can continue what he was doing before.
It seems the local notification must have an action, which opens activities. Is there anyway achieve what I wanted above?
I think you have to stop sending push notification when you app is in foreground
check if your app is in foreground or by following this link
check android application is in foreground or not?
check this in firebaseMessegingService class before sending PN and manage alert dialog appearance according that

GCM - Opening app from notification?

I'm working on an app which is using GCM for push notification.
My question is around how to open/start the app from when user clicks the notification.
I have two potential scenarios:
A) The app is open and is resident in either foreground or background, either way my GCM works fine, the GCM message arrives user taps notification and intent starts relevant activity (passed via the notification pending intent).
B) The app is closed and notification received, the user again taps the notification and the intent tries to start the relevant activity and this is where it gets messy.
The app has a back-end and so needs to auto-login/authenticate now if I try to start the same activity the credentials etc are out of date or dont exist therefore the app crashes. I need to launch the app from scratch in this case rather than simply start the particular activity via the pending-intent.
My question therefore is how do I tell the GCM listener to check if app running and if so simply start activity and if not (app is closed) launch app as normal?
thanks.
Ok solved this, not sure if its the best way but appears to work without issue.
What I ended up doing was creating a static boolean variable in launcher activity.
This is set true when app starts and the GCM listener tests for this variables state on incoming GCM notif (will always be true for life of app instance), if true go directly to activity else (app closed or stale) launch app.
This seems to work fine from what I can see, still interested in a better method as this does seem hackish?

Android - stay on notification list

I'm building an Android app which creates notifications. Each notification has a number of additional actions.
The default application (when the user taps on the notification) launches an activity in my app. That's working fine.
When the user swipes the notification to dismiss the notification, my app responds by doing some deletion work, and the user is left on the list of notifications. The one that the user swiped is removed from the list.
For some of my additional actions I want to launch activities, and that's working fine; for the other actions, I want my app to do some work but I also want the user to remain on the notification list. I'm struggling to find a way to do this last part - all my attempts to configure the intent and pending intent for my additional action result in behaviour whereby when the user clicks on the action, the notification list disappears.
Is there any way to get what I want (i.e. a notification additional action which performs work but leaves the user on the notification list)? Do I need to build a custom notification layout or something?

How to tell app was started by tapping push message notification

I have a Worklight app that receives push notifications from the server. A notification means there are new messages for the current user from other users. A user would read them by visiting a messages page within the app and then proceeding to a particular conversation page.
I would like to differentiate in the code between user intentions. The app would:
If the user started the app normally (not by tapping a new notification), present the user with the regular app home screen.
If the user started/resumed the app by tapping a notification, present the user with the messages page.
If received while the app is on foreground, only update the on-screen message count (regardless of which page is active).
The question is: is there a reliable means to differentiate between the above conditions?
In another thread, I saw a suggestion to remember the timestamp of a resume event and an onReadyToSubscribe event and assume we were asleep/inactive if notification arrives e.g. just a second after this. This would enable me to differentiate between 2 and 3.
However, 1 is not covered by this. I.e. if there have been new messages, but the user started the app normally, the app would have no means of knowing this and would think it was started by tapping a notification. This way, the user intended to see app home screen, but we transfer him to the messages page.
Is there a reliable way around this for both iOS and Android?
Worklight vesion 6.2.0.00-20140922-2259.
In a pure Native application, you can know "where from the user opened the app", meaning whether it was by tapping on a received notification or by tapping the application icon.
In a Worklight-based Hybrid application, and if using only the JavaScript API, this is not feasible because all paths arrive to the same destination which is the pushNotificationReceived function. So whether you tapped a notification or the app icon or brought the app to the foreground, the aforementioned function will be invoked.
The solution may be to base your implementation on the following Knowledge Center documentation topic: Using native and JavaScript push APIs in the same app.
This way, using a combination of native code and JS code, you could handle the scenario where a notification was received but the user decided to tap the application icon rather than the notification.
An end-to-end example would to somewhat involved to implement, but if you'll follow the code examples in the documentation topic, you should get there...

Categories

Resources