How to stop notification if app is in foreground - android

I have a scenario when i don't want android system to display push notification if app is in foreground.
Instead i want to display some view or launch activity in the app.
This is my code.
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//...
//...
sendNotification(remoteMessage.getNotification(), subtitle);
}
If i comment out sendNotification()method, it does not display notification on both background & foreground cases.

There are two types of messages data messages and notification
messages. Data messages are handled here in onMessageReceived whether
the app is in the foreground or background. Data messages are the type
traditionally used with GCM. Notification messages are only received
here in onMessageReceived when the app is in the foreground. When the
app is in the background an automatically generated notification is
displayed. When the user taps on the notification they are returned to
the app. Messages containing both notification and data payloads are
treated as notification messages. The Firebase console always sends
notification messages.
Thought you can only handle notification when app is in foreground.
Solution:-
Tell your server that app is in foreground and background and they will send notification when app is in foreground only

Related

Push notification received while the app is in the foreground is different from push notification received when the app is in background

I have implemented FCM push notifications in an android app.
While I am logged in to the app. I get the notification in the form that I am expecting as below.
When the app is in background I receive the json response instead as below.
Following is the code I have added in onMessageRecieved()
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("App")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap))/*Notification with Image*/;
How can I get the notification in the same manner in both cases.
Any help will be appreciated thank you
After setting up FCM in Android application, you can use Firebase console to send notifications. When foregrounded application receives a notification, onMessageReceived method is invoked. You should override this method to handle notification, but the problem is when the application is in the background and receives a notification, notification delivers to the device’s system tray and you can not handle notification with onMessageReceived method. When the user taps on a notification, it opens the app launcher by default. For example consider you want to do a specific task when a notification is received to the user or do something in background without the user realizing or don’t want to show notification dialog to the user, you can’t do these when the application is backgrounded.
Message Types
With FCM you can send two types of messages to clients :
1- Notification message, sometimes thought of a “display message”
2- Data message, which are handled by the client application
According to Google documents a notification message has a 2KB limit and a predefined user-visible keys. Data messages let developers send up to 4KB custom key-value pairs.
Solution:
If you want to handle notification when the application is backgrounded you should send data message and use onMessageReceived method.
Here is the complete article.
That is so because push notifications handles in foreground and background in different ways.
From the documentation
Notification messages delivered when your app is in the background. In
this case, the notification is delivered to the device’s system tray.
A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, both background and
foreground. In this case, the notification is delivered to the
device’s system tray, and the data payload is delivered in the extras
of the intent of your launcher Activity.
Also you have to remember that there are 2 types of firebase pushes - Notification message and Data message.
Looks like you are using Notification messages, so they are working as expected.
Just use data type of messages and so you can always handle them as you need.

Notification in background

When I open the app and send notification from the Firebase console , the onMessageReceived works fine. The app has its small icon , Notification sound ,etc.. But when the app is killed or is closed , no notification sound or a small icon appears.
After GCM the google changed the GCM as FCM, And FCM are containing two type of payload one is Notification and another One is Data payload when you are posting notification with Notification Payload then app will be in background then onMessageReceived not called but when in foreground onMessageReceived called, But if you put Data payload then all-time onMessageReceived is called.
Using Notification Payload
onMessageReceived called in the only foreground not in background
Using Data Payload
onMessageReceived called every time

Firebase custom data if App is Closed

I have used this How to get custom data from android firebase notification? solution for getting custom data from Firebase and it work only if Application is open, when Application is closed the custom data return empty String ""
Another question related if remove the code for notification from:
public void onMessageReceived(RemoteMessage remoteMessage) {
and application is closed i recevived always a notification but if open not?
I think if Application is open use my code but if closed what code use?
The callback onMessageReceived is only called when the application is running ie its in foreground.
according to https://firebase.google.com/docs/cloud-messaging/android/receive
onMessageReceived is provided for most message types, with the following exceptions:
Notification messages delivered when your app is in the background.
In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, both background and
foreground:
In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
So the payload data you need to fetch using intent.getExtras()

Push Notifications to Android App

I have an android app connected to FCM(firebase cloud messaging) for sending notifications . However , when the app is in background , it doesn't apply
custom notifications. I think this is because it does not enter OnMessageReceived method . Whats the solution for this ? As i read somewhere it can be done using HTTP Api but i am not sure of how to go about it ? Please help.Thank you.
Whether the method onMessageReceived is called is dependent on the notification payload.
See 'Handling Messages' section in FCM docs here.
Handling messages
onMessageReceived is provided for most message types, with the following exceptions:
Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
In summary, if the app is in foreground, then onMessageReceived will always be called. If the app is in background, then onMessageReceived will only be called if the notification payload is data only.
So to answer your question, if you must have onMessageReceived every time regardless if the app is in foreground or background state, then set your notification payload to contain data only.

when i send a push notification while my app is running,my app crashes

When i send a push notification while my app is running,my app crashes (via fire base console), but works just fine when my app is closed or running in the background.
Actually the problem is Getting in
class which is extends FirebaseMessagingService
FirebaseMessagingService this class is use to catch push notification.
so when your application is open you need to just check your code in
public void onMessageReceived(RemoteMessage remoteMessage)
Check your code in this method.
As per your second question when your application is closed or in background at the time OnMessageReceived() method is not handling a event of Push Notification
There are 2 main ways of getting a push notification from fire base
1) When application is in foreground (application is running or opened)
2) When application is in background (application is not running or closed)
In first case if you send push notification from fire base console the notification message and custom data payload is received in
public void onMessageReceived(RemoteMessage remoteMessage)
In short when application is in foreground onMessageReceived method will trigger always , notification data must be handled inside this method.
In second case if u send push notification from fire base console the notification message is received in system tray
and you can receive the data payload in the bundle extras of the launcher activity of your application.

Categories

Resources