How can I disable receiving notification in a specific activity in kotlin? - android

I have a Chat app, but I receive notification while I am in the chat activity.
I want to disable it and just get notification when I am not in the chat activity.

While you could somehow flag the app instance to not receive messages from FCM while it's showing a specific activity, that would be likely be quite unreliable as there are many factors affecting delivering.
The better approach would be to receive the message, but ignore it while the activity is showing, either in your onMessageReceived method or in code further downstream that handles it. Since notifications are only displayed by the system when your app is not active, ignoring the messages while the app is active will have the same effect as not receiving them, and is more reliable.

Related

Should I stop send push notification when another device foreground?

I want to show notification only app is close or background or in different chatroom. but I don't know which is better Solution.
stop send push notification when another device foreground
just send push notification anyway and don't show the notification in another device when app foreground.
In first solution I need to get another device information from server like in which chatroom or in which activity or in foreground it make my app and database more complex but maybe it reduce the network usage.
In second solution just send notification anyway and receiver device just ignore it but maybe big network usage.
Most apps that I know off use the second approach: they always send the FCM message, and only show it when needed. That also means that they usually only send a so-called tickle message, which contains very little data and whose main purpose it is to wake the app up.

Handling fcm notification when app not launched

I am using fcm push notification for my android app. I was able to display push notification on system tray when app is not launched. When I tap on the notification it opens the app launcher by default and I start an activity A from there. But the issue is, if I put the app to background and click on the app icon it again opens the app launcher rather than opening existing Activity A.
If the app process is killed, start the launcher activity. If the app is in the background, you can pass an intent to the notification which starts a DummyActivity that has no code on it, and immediatelly calls finish() on its onCreate() method. This will bring your app to the foreground.
Several things are not clear in your question. For example: How you send messages (from developer console or through rest api post requests to firebase backend)? What is your desired behaviour for app when push messages come? I will try to give you general answer that probably helps you to address issue and understand how to implement desired behaviour.
In any case, there are two types of Firebase push messages:
data messages
notification messages
more details about it check on Notification & data messages page
If you want to send additional details to activity that you are starting (something similar to bundle extras), you should use data messages and handle those in your service that extends FirebaseMessagingService by overriding onMessageReceived(RemoteMessage remoteMessage) method. This method is preferable for me because it is much more flexible. You can define all the details about showing notification based on received firebase message, including if notifications are bundled, what happens in details when user click notification and almost everything related to it.
If you don't need to start certain activity with some parameters, than you can use push messages and just define click_action. This method allows you to add define title, text and sound of notification (beside some other details) but it is not as flexible as if you send data messages
Here you can find detailed overview of possible parameters that you can use for different type of messages
Hope this helps

How do I get a call back for notification received in android?

Is it possible/is there a reliable way to get a call back when a notification is received on the android platform? i.e. I want to record when the notification is received (not when it is later opened).
No, It's your app which receives the message so if you need things logged, then log it. Simply handle it as you like once you receive it. Note that if you use FCM with Notification message message type, then you will not be directly notified about message arrival when app is in background (notification will be automatically posted by Firebase - your app will get message payload once use taps the notification). In such case consider switching to Data messages so you will get it no matter your app is in foreground or not.
See docs: https://firebase.google.com/docs/cloud-messaging/downstream

What component will be started when receiving a notification without clicking through

If there is an application receives a notification while it is not running in background/foreground. Will just receiving a notification trigger MyApplication.onCreate()? What other component will the android framework start by just receiving a notification? Thanks.
If you are talking about push notifications (which are supplied by GCM), Android will start your GCMIntentService, which you need for a working implementation (you can read more about it in the GCM integration documentation.
Before the GCMIntentService is called with the Intent, the Android OS receives a TCP packet from the Cloud Messaging servers, which contain all the data of the notification. Only 1 TCP connection is kept alive for all the notifications, to save power. Using the API keys, the OS will search for the application, which should receive the push. If it has been found, it will call the GCMIntentService of that application with the Intent containing the notification. From there on, it's up to the developer what he would like to do with that.
(To save even more power, the GCMIntentService is not running for all applications. Instead, a broadcast receiver has to be also defined, which wakes the service up when a notification arrives).
If you are talking about the Android notifications, which appear in the status bar, the application does not receive those. Those are posted via the NotificationManager system service by the apps themselves. The developers of the apps can create, update and cancel those notifications, based upon the app usage, and the events which happen.
When these notifications are created, you can add PendingIntents to them, which define the action which should happen when the user clicks on the notification. Mostly it is an intent to open a specific Activity of the application. When the specific Activity has been opened, it can check its getIntent(), which contains the intent of the notification, and any extra data which has been added to it. (Intents can contain extras, like Strings, Integers, Parcelables, etc.).
A common use case in Android apps is receiving a GCM message, and then posting a notification in the status bar. Like when you get an email in GMail. This way the user is not interrupted in his current work, but can still open the app if he wants to.

Handling Parse Push Notifications in Android

I am using Parse API in order to handle push notifications. In our Android application, I want to accomplish two things:
1) If we have received a Push Notification with the application is closed and the user clicks on the notification, I want to be able to understand that the application is being opened via a push notification.
2)If we receive a push notification while the application is open, I want to handle this and do some extra work.
In both cases, I want to be aware that the application has received a push notification in order to execute some special operations.
As far as I understand from Parse API documentations, it offers two methods of handling pushes: Responding with an Activity and Responding with an Intent. I am currently calling
PushService.setDefaultPushCallback(context, MainActivity.class);
in my Application class with needed changes in the AndroidManifest.xml file and already receive push notifications, this corresponds to Responding with an Activity method. But I don't know how to be aware of Push Notifications explicity with this method.
Thanks in advance.
When a push is received ,Check
1:Whether our application is in foreground or background.
If it is foreground, that means app is visible and do your stuff(show alerts or anything you want).
If app is in background,that means it is not visible and if you want to do any thing based on this.
i hope this helps..

Categories

Resources