What is the best way to handle both Notification Messages and Data Messages in firebase using Xamarin Android, while the user is in Foreground and Background?
Also, how do i get the notification data, for example, the text of a particular notification?
PS: I have visited the following threads and none actually helped :
When device screen off then how to handle firebase notification?
Well, I found the answers to my own question so I'm posting the answer for someone who is looking for firebase integration in xamarin.
Install
Xamarin.Firebase.Messaging
package to your project.Add the following code to your manifest.xml to receive firebase notifications.
Now to get the registration token from firebase add a class file and add the following code to it:
Here
FirebaseInstanceId.Instance.Token
gets the instance token for the current device, Also the methodSendRegistrationToServer
can be used to send the token to send the token to a server.Now add another class to handle notifications on foreground
Here the method
SendNotification
is used to explicitly send a notification to the system tray, Since push notifications while the device is in the foreground are not automatically displayed in the system tray.When the device is in the background or killed notification is automatically generated and by default the main launcher activity is loaded, to get the data from the background notification you need to use intent as follows(On your main launcher activity):
Also if google play services are not up to date this code might crash your application so to check if google play services are available or not do this :
Check the below link for information on how to add your project to the firebase console:
https://developer.xamarin.com/guides/android/data-and-cloud-services/google-messaging/remote-notifications-with-fcm/