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>