将令牌 ID 发送到服务器端安卓



我想在不使用Firebase控制台的情况下发送通知(我想使用服务器端(。为此,我正在尝试将设备令牌ID发送到Web服务。我正在这个 Android 项目中使用 Firebase Cloud Messaging 。我该怎么做?

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 ??
    }

谢谢!

1(像这样创建火力基地参考

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

欲了解更多信息,请阅读这篇文章

首先,

您应该运行一些服务器来接收令牌和存储。然后,您可以从应用程序进行 API 调用,以将 FCM 令牌发布到您的服务器。

将下面给出的这段代码粘贴到您的类中,此方法将自动从"onTokenRefresh(("方法中获取。只需确保将 Web 服务 URL 替换为您自己的服务器的 Web 服务 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);
}

相关内容

  • 没有找到相关文章

最新更新