Reputation: 1839
I have created an app where I have chat in it.
I would like that the users will receive a notification that they got a new message whenever they are outside of the app.
In order to do so, I'm using Firebase
service.
Now, whenever a message is sent, I call this method:
I have created the following MyFirebaseMessagingService
class to test when it is called:
private class MyTask extends AsyncTask<MyTaskParams, Void, Void> {
@Override
protected Void doInBackground(MyTaskParams... params) {
String userDeviceIdKey = params[0].url;
String title = params[0].title;
String body = params[0].body;
String authKey = "XXXXX"; // You FCM AUTH key
String FMCurl = "https://fcm.googleapis.com/fcm/send";
URL url;
try {
url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
json.put("to",userDeviceIdKey);
json.put("priority","high");
JSONObject info = new JSONObject();
info.put("title", title); // Notification title
info.put("body", body); // Notification body
info.put("var1", chatID);
info.put("var2",receiverID);
info.put("var3",itemID);
json.put("data", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
Log.w("", "getInstanceId failed" + json);
conn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
and In my Service I created:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("TESTING_MESSAGE", "" + remoteMessage);
}
}
However, It seems like I never get this Log
when I send a message and it doesnt look like onMessageReceived
is ever called.
Is there any reason?
My next goal is to make notification inside onMessageReceived
but I want to make sure first that I get there.
My manifest is:
<service
android:name=".service.MyFirebaseMessagingService"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
Upvotes: 0
Views: 171
Reputation: 78
I make a demo app with your code, and it works well and I can see that onMessageReceived function is trigged when a message arrives.
I use the firebase-messaging version 20.1.7
implementation 'com.google.firebase:firebase-messaging:20.1.7'
so I think you should do the following checking:
check if your app is in the foreground. because you can't trigger onMessageReceived when the app is in the background. refer: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages
check if you wrote the right device token(your field 'userDeviceIdKey') when you sent the http request.
check if your http request has some error in catch.
hope can help you.
Upvotes: 1
Reputation: 74
First of all check what you send: data message or notification message. They may look the same, but depending on what type of message, the different method called. Check this out: https://firebase.google.com/support/faq/#fcm-android-background
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Map test = remoteMessage.getData();
Log.d(TAG, "Message data payload: " + test);
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " +
remoteMessage.getNotification().getBody());
Upvotes: 0
Reputation: 11
The onMessageReceived()
method will never be called if the app is killed.
Firebase onMessageReceived not called when app in background
Upvotes: 0