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 background and notification arrives then the default notification comes and doesn't run my code of onMessageReceived
.
Here is my onMessageReceived
code. This invokes if my app is running on foreground, not when app in background. How to 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]
To capture the message in background you need to use a
BroadcastReceiver
and add this to your manifest:
Thanks to All of you for your Answers. But I solved this by sending data message instead of sending Notification. Server code
And caught the Data in onMessageReceived
Using this code you can get the notification in background/foreground and also put action:
In-App use this code:
According to docs
Edit :
Based on this thread :
You can't set click_action payload using Firebase Console. You could try testing with a curl command or a custom http server
Simple summary like this
if your app is running;
is triggers.
if your app is not running (killed by swiping) ;
is not triggered and delivered by direclty. If you have any specialy key-value pair. They don' t work beacuse of onMessageReceived() not working.
I' ve found this way;
In your launcher activity, put this logic,
in this if block, search for your keys according to firebase UI.
In this example my key and value like above; (sorry for language =))
When my code works, i get "com.rda.note".
with this line of code, i closed my application and open Google Play Market
happy coding =)
The easy way to send messages even if the app is in background and foreground as follow:- To send a message using API, you can use a tool called AdvancedREST Client, its a chrome extension, and send a message with the following parameters.
Rest client tool Link: https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
use this url:- https://fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key=Your Server key From or Authoization key(see below ref)
Authorization key can be obtained by visiting Google developers console and click on Credentials button on the left menu for your project. Among the API keys listed, the server key will be your authorization key.
And you need to put tokenID of the receiver in the “to” section of your POST request sent using API.