本文介绍了Android GCM将令牌发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

给出了一个将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注​​册令牌:+令牌);

// TODO:实施此方法将任何注册发送到您应用的服务器。
sendRegistrationToServer(token);
...
} catch(Exception e){
...
}
}

/ **
*坚持注册到第三方服务器。
*
*修改此方法以将用户的GCM注册令牌与应用程序维护的任何服务器端帐户
*相关联。
*
* @param token新的令牌。
* /
private void sendRegistrationToServer(String token){
//根据需要添加自定义实现。


$ / code $ / pre

但这是在 IntentService 只要 onHandleIntent 返回正确结束?因此,如果启动http调用以发送具有流行的库的令牌,我甚至没有看到我的 onStart 命中:

  private void sendRegistrationToServer (String token){
post(/ devices,params,new AsyncHttpResponseHandler(){
// TODO:确认ONSTART实际上是要求确保请求至少上升到上游,即使我不做在INTENTSERVICE中收到回拨
//如果不然,可能不需要将INTENTSERVICE更改为设备注册服务
@Override
public void onStart(){
// not实际使用回调,因为从intentservice发送的请求
Log.d(tagzzz,sending upstream);
}

@Override
public void onSuccess(int statusCode,Header []头,byte [] responseBody){
//实际上没有使用cal因为从intentservice发送的请求

$ b @Override
public void onFailure(int statusCode,Header [] headers,byte [] responseBody,Throwable error){
//实际上并没有使用回调,因为从intentservice发送的请求
}
});
}

我的http请求会在 onHandleIntent之前发送到上游返回并完成 IntentService ?如果没有,Google为什么将这个作为他们的示例将您的令牌发送到服务器? >在onHandleIntent返回并完成IntentService之前,我的http请求是否会被上游发送?

考虑到您使用的是名为android-async-http的库,我会假设默认行为是针对它的异步执行HTTP。不确定 post()调用是否会在 onHandleIntent()返回之前完成其工作,但似乎不可能的。

Given that you are using a library named "android-async-http", I would assume that the default behavior is for it to execute the HTTP asynchronously. It is indeterminate whether or not the post() call will complete its work before onHandleIntent() returns, but it seems unlikely.

Google doesn't. Google has a stub sendRegistrationToServer(), as you can see in your first code listing. I am not aware of any Google examples that use the "android-async-http" library.

You decided to use an asynchronous mechanism for sending that HTTP request. That is an inappropriate choice for inside an IntentService. Now, perhaps that library has a synchronous option, in which case you could switch to that. Otherwise, use something else synchronous for the HTTP request (HttpURLConnection, OkHttp3, etc.) from the IntentService.

Note that Volley is not a great choice here, insofar as Volley is also strongly tilted towards asynchronous work.

这篇关于Android GCM将令牌发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:36