As per https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
, I can use data messages which will be 100% received onMessageReceived method and act accordingly. But in my case none of my messages are received when app is killed. Though they are sometimes received when I'm testing on Nexus 5 only. Below is my data payload.
05-16 06:46:01.663 13199-29574/jss.test E/MyFirebaseMsgService: Data Payload: {data={"image":null,"title":"My message"}}
05-16 06:46:01.667 13199-29574/jss.test E/Providerfalse: true
05-16 06:46:01.667 13199-29574/jss.test E/MyFirebaseMsgService: Notification JSON {"data":{"image":null,"title":"My message"}}
Manifest For FCM as below:
<service
android:name=".FirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Firebasemessaging service:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
sendPushNotification(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
//this method will display the notification
//We are passing the JSONObject that is received from
//firebase cloud messaging
private void sendPushNotification(JSONObject json) {
//optionally we can display the json into log
Log.e(TAG, "Notification JSON " + json.toString());
try {
//getting the json data
JSONObject data = json.getJSONObject("data");
//parsing json data
String title = data.getString("title");
String message = data.getString("message");
String imageUrl = data.getString("image");
//code to call service if message has specific data values.
}
catch (Exception e) {
e.printStackTrace(); }}
Can you guide where I fix to make it working perfectly.