How to prevent duplicate notifications in Firebase Cloud Messaging (FCM) - android

I'm currently implementing a Chat application using Firebase Cloud Messaging to send push notifications. Using the notification field in the API call, Firebase displays them automatically without having to manually create a service and listen for the messages.
The push notification is pretty generic, just says "You have a new message!", so it doesn't make sense to keep adding new pushes every new message, I need firebase to not show a new push if the message body contents the same of a previous one.
Is it posible to do without implementing the service and handling Notification show manually? Found no references of this use case in the documentation.

If your application is in Foreground then you receive the notification in onReceive of your FirebaseMessagingService. Else notification is delivered to system notification tray. Since you don't know the id of the notification so you may not retrieve it. It is also possible that your application is not running at the time you receive the notification. So logically it is NOT possible even by implementing the service. Well you can solve the problem by another approach. You can use Firebase Database in conjuction with Cloud Functions. Just have a look on developer guide of cloud functions and you will find that they can help yourself achieve you what you want. As a solution skeleton : Post messages to Firebase Database and Send notification using cloud functions. When the recipient reads the message update the database to reflect message has been read like having a variable seen. Design your cloud function such that it reads the value of seen value of last message and sends notification only if it was true. Hope this helps.

For manual handling you have to use the service. Go for handleIntent(intent) of FirebaseMessagingService.
This method gets called when application is in foreground, background and killed state. To avoid the duplication, do not call super.handleIntent(intent). This will prevent the automatic push notification when app in BG or killed state.
This worked for me.

Related

How to get Android push notification payload when background App is launched by user click on notification

I have an Android app that's in the background. I've registered it with Firebase and have been able to successfully retrieve it's token, as well as sending messages to it via the Firebase Console Manager (FCM).
When I send test messages via FCM, when the app is in the foreground the
onMessageReceived(remoteMessage:RemoteMessage)
call back API for my FirebaseMessagingService derived class gets triggered. I can then call remoteMessage.notification!!.body and obtain it's payload. This is the documented behavior so it's working as expected.
When the app is in the background though (say I press the home key), and I send the test message, that event doesn't get called but instead a notification shows up on the Android device, which is also the expected behavior. So far so good.
However, when I click on the notification in the notification channel, it brings the app to the foreground. However, when the app is launched like that - how does one retrieve the push notification payload? The onMessageReceived isn't triggered, and I'm having a hard time finding the answer to this in Android's documentation.
Any indication as to how to obtain that?
Thanks
Turns out this question provided the answer i was looking for. TL;DR - look in the extras field in the intent variable of the Activity
Firebase (FCM): open activity and pass data on notification click. android
Im not sure my solution below is best. But I think you can save the message to share preferences . And get it to another place it you want to you.
override fun onMessageReceived(p0: RemoteMessage) {
//TODO save it to share preferences
}
Give me another better solution if you find it. Thank you so much.

Firebase notification works even with no service or inherited class?

I implemented firebase into my app to use it's notification service a while back and it worked, and today decided to use it's key/value feature, but realized that no matter what I write in my class inherited from FirebaseMessagingService, nothing happens. So I decided to remove the my class for dubugging purposes, and I still got notifications. So I removed the firebase messaging service from the manifest, deleted the app cache and ran the project, but I still get notifications! The only way I can prevent notifications from coming is to remove the firebase dependency in gradle. What's happening here?
Just read the documentation.
...
Ok, let's go: there are three kind of "notification" using Firebase: notification messages, data messages and messages with both notification and data. Each is received and handled differently by the system. Here you can see more information about message types (how there are built).
There are handled like that:
Notification message
App in foreground: onMessageReceived's implementation
App in background: system tray (system dispatch automatically notification) -> with or without service implementation (maybe your case, without code I can't know)
Data message
App in foreground: onMessageReceived's implementation
App in background: onMessageReceived's implementation
Message with both notification and data
App in foreground: onMessageReceived's implementation
App in background: system tray (notification) and in extras of the intent (data)
But without any code or notification information, I can't continue to help you. Until you provide some code example, I only think that you send a notification message, and so system automatically display a notification and there is no call to the service's onMessageReceived, it's normal.
If you want to "control" and decide if notification should be shown or not, just send data messages, and in your onMessageReceived implementation, create a notification with content and intent and notify it to the system (if you want it to be shown).

Firebase Pull notification

I'm working with Firebase Cloud Messaging. I have a couple of question I was not able to understand from the documentation:
Android: Lets suppose the app is closed (not backgrounded: closed). If I send a notification with also the data payload, this data payload is passed to the activity through the Intent Extra
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.
What if the user does not tap on the notification? Is the data payload lost? Is there a way to retrieve it?
iOS/Android. Lets suppose the user disabled the notification and I sent a notification to the client: is there a way to retrieve (pull) the notification at the application start?
Thanks very much
If the app is closed (not in background) the onMessageReceived method is called when the notification is received and there you can retrieve the data payload with remoteMessage.getData().get("key_for_parameter"); where key_for_parameter is the name of the parameter that you send in the notification. This method is called even if the application is closed. However, take into account that the "onMessageReceived" is called only if you omit the "notification" param in the notification (look this post)
So once you have the params in the onMessageReceived you can look for an strategy to use them in your application like storing in the DB and you are not going to lose the data if the user does not click on the notification.
if your app in background, Firebase will not trigger onMessageReceived(). Why.....? I have no idea. In this situation, I do not see any point in implementing FirebaseMessagingService.
According to docs, if you want to process background message arrival, you have to send 'click_action' with your message. But it is not possible if you send message from Firebase console, only via Firebase API. It means you will have to build your own "console" in order to enable marketing people to use it. So, this makes Firebase console also quite useless!
There is really good, promising, idea behind this new tool, but executed badly.
I suppose we will have to wait for new versions and improvements/fixes

The right approach to listen to GCM notifications

I'm trying to build a GCM notification listener, which will basically use the notification to flag the user that some operation should be made (which involve communicating with my remote App-Server).
I assumed that I should create a UI-less application running on the device's startup and listen to the GCM notifications and issue the internal android notification. When the user opens the notification an activity will be opened which will do the rest of the job with the remote App-Server.
Looking at notification examples it seems to me that I may be missing some basic understanding since all te examples which I had found use a UI application to manipulate the notifications.
What do I miss?
The common use case for handling of GCM messages in Android apps is as follows :
Your app registers to GCM upon startup and sends the registration ID to your server.
Your server sends a GCM message to your app.
You app receives the message in a broadcast receiver, which usually starts an intent service.
The intent service usually displays a notification to the user.
The user taps the notification, which starts an activity of the app.
You can see this use case implemented in the official GCM demo and in many other examples.
The fact that the app you wish to develop has no UI doesn't prevent you from implementing the exact same use case.

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