GCM - Opening app from notification? - android

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?

Related

Deep link notifications not working in android 12 (Not opening the app when user clicks on notification)

It was working fine, the app was also in production. But recently we noticed that on android 12 when the user clicks on the notification it doesn't open the app and navigated the fragment/activity which the deep link has.
But as soon as the user opens the app the deep link gets fired(if the user has previously clicked on the notification then) and moves user to the destination which was in the deep link. And also if the user is in the app and clicks on the notification it will work fine. But if the user is outside the app it doesn't work in that case. We also tried to upgrade the firebaseBomVersion to the newest version (31.0.2). But nothing seems to work.
There are online other post related to deep link not working on android 12, but they all have are having different issues.
Does anybody know anything about this issue?
So, in the end, the problem was this
system_process E/NotificationService: Indirect notification activity start (trampoline) from com.xyz.debug blocked
On android 12 and above we can't start activities from a service or from a broadcast receiver on a notification tap (which is what I was here doing), I was trying to start an activity from the broadcast receiver, and I was firing that broadcast when the user was tapping on the notification.
When users interact with notifications, some apps respond to
notification taps by launching an app component that eventually starts
the activity that the user finally sees and interacts with. This app
component is known as a notification trampoline.
To improve app performance and UX, apps that target Android 12 or
higher can't start activities from services or broadcast receivers
that are used as notification trampolines. In other words, after the
user taps on a notification, or an action button within the
notification, your app cannot call startActivity() inside of a service
or broadcast receiver.
When your app tries to start an activity from a service or broadcast
receiver that acts as a notification trampoline, the system prevents
the activity from starting, and
Documentation link
How did I solve the problem
simple, I used the same code, i.e. adding a broadcast to the pending intent and attaching that pending intent to the setContentIntent() on the notification for android 11 and lower devices but for android 12 and higher devices I replaced that broadcast to activity intent (an activity with transparent background) and in that activity, I placed the same broadcast code which I used for android 11 and lower devices and then using sendBroadcast() method I fired that intent myself and after that finished that activity.

How to get notification data when the app launched from launcher icon

I have implemented a service which extends FirebaseMessagingService and i am using it when the app is on the foreground to the received notification with my custom in-app notification view. And when the app is in background, as all other FCM releated topics and documentation here suggests, it is handled by the system tray, i only need to get the data from the bundle on the launched activity and make my redirections etc. on there.
What i also need to do is, when a notification is received and the app is launched by tapping on the launcher icon, not by notification. I need to get the notification data in that case and again make my redirections accordingly. But i couldn't able to get the attached data in that case. Any idea how to get the notification data on regular app launch?
Okay, this is how i solved my issue for above case, it is more like a workaround than a solution, but maybe someone might find it useful for his/her own purposes:
If the app was in the background when the notification is received and it is launched from the app icon but not notification, then i catch it inside the handleIntent method of the service that extends FirebaseMessagingService to store the received data on my local.
If the app was in the background when the notification is received and it is launched from the notification but not from the app icon, then i get the notification data from the getIntent().getExtras() of SplasActivity (which is my LAUNCHER activity)
If the app was in the foreground when the notification is received, the i show it as an in-app notification without storing anything to my local
And i delete the stored data, after i show the incoming message as in-app notification on my next app launch either by notification or from app launcher icon.
There is definetely downsides of this implementation, but like i said it is more like workaround than a solution. I guess a proper solution should send the data as a data message as it is described in this post. But unfortunately that was not an option for me.

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 - 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

How to avoid processing push notifications from GCM when the app is closed

I am integrating push notifications on an app with this special behaviour:
The app would only proccess the notification if it is open (foreground or background)
My problem comes when the user closes the app using the recent application stack (or something similar) because the onDestroy() method is not always called and the app can't delete the token from GCM. If I send a push notification at this moment the application proccesses it.
I have been looking for this issue and seems that everyone have the opposite problem, they don't receive notifications when the app is closed.
Is there anyway to configure the broadcast receiver to not wake up my application when It is closed? Or any method to achieve this?
Thank you
There are two way to achieve this,
First Method: Call this when you clear you app from stack,I mean in you application task,OnDestroy or OnTerminate.This will clear your Device token so you will not receive notification any more.
But you have to make sure that when you come back in your application you need to register again and send your device token to server so you can receive notification back normally.
GCMRegistrar.unregister(context);
Second Method:
You can use sharedpreferences, You can create it with Boolean value.You have to set value true when you start you application and false when you clear it.
Then in you GCMIntentService class you can use this preference at onMessage method.you can set condition over there that If the value is true then it will fire notification otherwise nothing will display.
In this case server will fire notification but you notification will not display in notification bar.
Hope the info will helpful for you.
Use Broadcast receivers. register it in onStart and unregister in onStop of Activity.
If you have more than one activity and doing something from within the GCM IntentService which you want to prevent if you app isn't running then maintain a count in shared preferences and increase it in OnStart and decrease in onStop in all Activities. and when you get a GCM check if the count is positive to insure that your app is in foreground.

Categories

Resources