Android GCM 正在向服务器发送令牌
Android GCM Sending token to server
GCM Sample Project 给出了一个向您的服务器发送 GCM 令牌的简单示例:
public class RegistrationIntentService extends IntentService {
...
@Override
protected void onHandleIntent(Intent intent) {
try {
...
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.i(TAG, "GCM Registration Token: " + token);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(token);
...
} catch (Exception e) {
...
}
}
/**
* Persist registration to third-party servers.
*
* Modify this method to associate the user's GCM registration token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
}
但是这是在 IntentService
中完成的,onHandleIntent
returns 就完成了,对吗?因此,如果启动 http 调用以使用流行的 android-async-http 库发送令牌,我什至看不到我的 onStart
命中:
private void sendRegistrationToServer(String token) {
post("/devices", params, new AsyncHttpResponseHandler() {
// TODO: MAKE SURE ONSTART ACTUALLY CALLED TO MAKE SURE REQUEST AT LEAST GOES UPSTREAM EVEN IF I DON'T RECEIVE A CALLBACK SINCE IN INTENTSERVICE
// IF NOT THEN MIGHT HAVE TO CHANGE THE INTENTSERVICE TO A SERVICE FOR DEVICE REGISTRATION
@Override
public void onStart() {
// not actually using callback because request sent from intentservice
Log.d("tagzzz", "sending upstream");
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
// not actually using callback because request sent from intentservice
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
// not actually using callback because request sent from intentservice
}
});
}
我的 http 请求是否会在 onHandleIntent
returns 之前向上游发送并完成 IntentService
?如果不是,为什么 Google 将此作为将令牌发送到服务器的示例?
Will my http request even be sent upstream before onHandleIntent returns and finishes the IntentService?
鉴于您正在使用名为 "android-async-http" 的库,我假设默认行为是它异步执行 HTTP。不确定 post()
调用是否会在 onHandleIntent()
returns 之前完成其工作,但似乎不太可能。
why does Google give this as their example for sending your token to your server?
Google 没有。 Google 有一个存根 sendRegistrationToServer()
,如您在第一个代码清单中所见。我不知道有任何使用 "android-async-http" 库的 Google 示例。
您 决定使用异步机制发送该 HTTP 请求。对于 IntentService
内部来说,这是一个不合适的选择。现在,也许那个库有一个同步选项,在这种情况下你可以切换到那个。否则,对来自 IntentService
.
的 HTTP 请求(HttpURLConnection
、OkHttp3 等)使用其他同步内容
请注意,Volley 在这里并不是一个很好的选择,因为 Volley 也强烈倾向于异步工作。
GCM Sample Project 给出了一个向您的服务器发送 GCM 令牌的简单示例:
public class RegistrationIntentService extends IntentService {
...
@Override
protected void onHandleIntent(Intent intent) {
try {
...
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.i(TAG, "GCM Registration Token: " + token);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(token);
...
} catch (Exception e) {
...
}
}
/**
* Persist registration to third-party servers.
*
* Modify this method to associate the user's GCM registration token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
}
但是这是在 IntentService
中完成的,onHandleIntent
returns 就完成了,对吗?因此,如果启动 http 调用以使用流行的 android-async-http 库发送令牌,我什至看不到我的 onStart
命中:
private void sendRegistrationToServer(String token) {
post("/devices", params, new AsyncHttpResponseHandler() {
// TODO: MAKE SURE ONSTART ACTUALLY CALLED TO MAKE SURE REQUEST AT LEAST GOES UPSTREAM EVEN IF I DON'T RECEIVE A CALLBACK SINCE IN INTENTSERVICE
// IF NOT THEN MIGHT HAVE TO CHANGE THE INTENTSERVICE TO A SERVICE FOR DEVICE REGISTRATION
@Override
public void onStart() {
// not actually using callback because request sent from intentservice
Log.d("tagzzz", "sending upstream");
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
// not actually using callback because request sent from intentservice
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
// not actually using callback because request sent from intentservice
}
});
}
我的 http 请求是否会在 onHandleIntent
returns 之前向上游发送并完成 IntentService
?如果不是,为什么 Google 将此作为将令牌发送到服务器的示例?
Will my http request even be sent upstream before onHandleIntent returns and finishes the IntentService?
鉴于您正在使用名为 "android-async-http" 的库,我假设默认行为是它异步执行 HTTP。不确定 post()
调用是否会在 onHandleIntent()
returns 之前完成其工作,但似乎不太可能。
why does Google give this as their example for sending your token to your server?
Google 没有。 Google 有一个存根 sendRegistrationToServer()
,如您在第一个代码清单中所见。我不知道有任何使用 "android-async-http" 库的 Google 示例。
您 决定使用异步机制发送该 HTTP 请求。对于 IntentService
内部来说,这是一个不合适的选择。现在,也许那个库有一个同步选项,在这种情况下你可以切换到那个。否则,对来自 IntentService
.
HttpURLConnection
、OkHttp3 等)使用其他同步内容
请注意,Volley 在这里并不是一个很好的选择,因为 Volley 也强烈倾向于异步工作。