Here is my manifest:
<service android:name=".fcm.PshycoFirebaseMessagingServices"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <service android:name=".fcm.PshycoFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service>
When the app is in the background and a notification arrives, then the default notification comes and doesn't run my code of onMessageReceived
.
Here is my onMessageReceived
code. This is invoked if my app is running on the foreground, not when it is running in the background. How can I run this code when the app is in background too?
// [START receive_message] @Override public void onMessageReceived(RemoteMessage remoteMessage) { // TODO(developer): Handle FCM messages here. // If the application is in the foreground handle both data and notification messages here. // Also if you intend on generating your own notifications as a result of a received FCM // message, here is where that should be initiated. See sendNotification method below. data = remoteMessage.getData(); String title = remoteMessage.getNotification().getTitle(); String message = remoteMessage.getNotification().getBody(); String imageUrl = (String) data.get("image"); String action = (String) data.get("action"); Log.i(TAG, "onMessageReceived: title : "+title); Log.i(TAG, "onMessageReceived: message : "+message); Log.i(TAG, "onMessageReceived: imageUrl : "+imageUrl); Log.i(TAG, "onMessageReceived: action : "+action); if (imageUrl == null) { sendNotification(title,message,action); } else { new BigPictureNotification(this,title,message,imageUrl,action); } } // [END receive_message]
Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.
Handling background message notifications To add the handler, make the following changes on App. js before App() : // Register background handler // Get the notification messaging(). setBackgroundMessageHandler(async remoteMessage => { // Extract the body let message_body = remoteMessage.
To make firebase library to call your onMessageReceived() in the following cases
you must not put JSON key notification
in your request to Firebase API but instead, use data
, see below.
The following message will not call your onMessageReceived() when your app is in the background or killed, and you can't customize your notification.
{ "to": "/topics/journal", "notification": { "title" : "title", "text": "data!", "icon": "ic_notification" } }
but instead using this will work
{ "to": "/topics/dev_journal", "data": { "text":"text", "title":"", "line1":"Journal", "line2":"刊物" } }
Basically, the message is sent in the argument RemoteMessage
along with your data object as Map<String, String>
, then you can manage the notification in onMessageReceived
as in the snippet here
@Override public void onMessageReceived(RemoteMessage remoteMessage) { Map<String, String> data = remoteMessage.getData(); //you can get your text message here. String text= data.get("text"); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) // optional, this is to make beautiful icon .setLargeIcon(BitmapFactory.decodeResource( getResources(), R.mipmap.ic_launcher)) .setSmallIcon(smallIcon) //mandatory ....... /*You can read more on notification here: https://developer.android.com/training/notify-user/build-notification.html https://www.youtube.com/watch?v=-iog_fmm6mE */ }
There are two types of messages in FCM (Firebase Cloud Messaging):
onMessageReceived()
callback only when your app is in foreground onMessageReceived()
callback even if your app is in foreground/background/killed NOTE: Firebase team have not developed a UI to send
data-messages
to your devices, yet. You should use your server for sending this type!
To achieve this, you have to perform a POST
request to the following URL:
POST
https://fcm.googleapis.com/fcm/send
Content-Type
, Value: application/json
Authorization
, Value: key=<your-server-key>
{ "to": "/topics/my_topic", "data": { "my_custom_key": "my_custom_value", "my_custom_key2": true } }
{ "data": { "my_custom_key": "my_custom_value", "my_custom_key2": true }, "registration_ids": ["{device-token}","{device2-token}","{device3-token}"] }
NOTE: Be sure you're not adding JSON key
notification
NOTE: To get your server key, you can find it in the firebase console:Your project -> settings -> Project settings -> Cloud messaging -> Server Key
This is how you handle the received message:
@Override public void onMessageReceived(RemoteMessage remoteMessage) { Map<String, String> data = remoteMessage.getData(); String myCustomKey = data.get("my_custom_key"); // Manage data }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With