Send Token ID to server side Android

被刻印的时光 ゝ 提交于 2019-12-06 15:00:50

问题


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:


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




回答2:


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.




回答3:


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);
}


来源:https://stackoverflow.com/questions/44011260/send-token-id-to-server-side-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!