I want to send notifications without using Firebase console (I want to make with server side). For this,I am trying to send device token id to web service. I am using Firebase Cloud Messaging in this Android project. How can I do this?
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService{
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Token: " + token);
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
// send token to web service ??
}
Thank you !
1) creat firebase ref like this one
private void sendRegistrationToServer(String token) {
// send token to web service ??
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/IDs");
// then store your token ID
ref.push().setvalue(token)
}
For more read this post
First you should have some server running to receive your token and store. Then you can make an API call from your App to post FCM token to your server.
Paste this code given below in your class and this method will be exected from "onTokenRefresh()" method automatically. Just make sure you replace the web-service url with your own server's web-service url.
private void sendRegistrationToServer(String deviceToken) {
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String deviceToken = params[0];
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://lib-dm.process9.com/libertydm/ValidateUserHandler.ashx");// replace with your url
httpPost.addHeader("Content-type",
"application/x-www-form-urlencoded");
BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair(
"UserId", paramUsername); // Make your own key value pair
BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair(
"Password", paramPassword);// make your own key value pair
// You can add more parameters like above
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(usernameBasicNameValuePair);
nameValuePairList.add(passwordBasicNameValuePair);
try {
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(
nameValuePairList);
httpPost.setEntity(urlEncodedFormEntity);
try {
HttpResponse httpResponse = httpClient
.execute(httpPost);
InputStream inputStream = httpResponse.getEntity()
.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
stringBuilder.append(bufferedStrChunk);
}
return stringBuilder.toString();
} catch (ClientProtocolException cpe) {
System.out
.println("First Exception coz of HttpResponese :"
+ cpe);
cpe.printStackTrace();
} catch (IOException ioe) {
System.out
.println("Second Exception coz of HttpResponse :"
+ ioe);
ioe.printStackTrace();
}
} catch (UnsupportedEncodingException uee) {
System.out
.println("An Exception given because of UrlEncodedFormEntity argument :"
+ uee);
uee.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(token);
}