Android GCM将令牌发送到服务器 [英] Android GCM Sending token to server

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

问题描述

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调用以发送具有流行的 android-async-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()返回之前完成其工作,但似乎不可能的。


为什么Google会以此为例将您的令牌发送到您的服务器?
blockquote>

Google没有。 Google有一个存根 sendRegistrationToServer(),就像你在第一个代码清单中看到的那样。我不知道任何使用android-async-http库的Google示例。


$ b

决定使用异步机制发送该HTTP请求。对于 IntentService 内部,这是一个不恰当的选择。现在,也许该库有一个同步选项,在这种情况下,您可以切换到该选项。否则,从 IntentService 中使用其他同步的HTTP请求( HttpURLConnection ,OkHttp3等)。 p>

请注意,Volley并不是一个很好的选择,因为Volley也非常倾向于异步工作。


The GCM Sample Project gives a stubbed out example of sending a GCM token to your server:

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

but this is done in an IntentService which finishes as soon as onHandleIntent returns right? So if starting an http call to send the token with the popular android-async-http library, I'm not even seeing my onStart hit:

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

Will my http request even be sent upstream before onHandleIntent returns and finishes the IntentService? If not, why does Google give this as their example for sending your token to your server?

解决方案

Will my http request even be sent upstream before onHandleIntent returns and finishes the IntentService?

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.

why does Google give this as their example for sending your token to your server?

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将令牌发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆