2012-12-04 57 views
3

我在我的应用程序中嵌入GCM推送通知。我面临一个非常奇怪的问题,第一次运行时,我无法获得GCM注册令牌,但是当您第二次运行我的应用程序时,您将在控制台上获得注册ID打印。我不知道我在做什么。这是我迄今为止所做的。Android GCM:未在onCreate()方法上获取注册令牌

这是我的onCreate()方法,我想打印GCM的RegID:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final String regId = GCMRegistrar.getRegistrationId(this); 
    GCM_regID = regId; 
      System.out.println("GCM regId: "+GCM_regID); 

否则里面的onCreate()下面的代码:

/** 
    * Google Cloud Messaging - Getting server Url and device ID to send it 
    * across Google server for the notification.. 
    */ 
    mGCMReceiver = new GCMReceiver(); 
    mOnRegisteredFilter = new IntentFilter(); 
    mOnRegisteredFilter.addAction(Constants.ACTION_ON_REGISTERED); 

    if (Constants.SENDER_ID == null) { 
     // mStatus.setText("Missing SENDER_ID"); 
     return; 
    } 
    if (Constants.SERVER_URL == null) { 
     // mStatus.setText("Missing SERVER_URL"); 
     return; 
    } 

    GCMRegistrar.checkDevice(this); 
    GCMRegistrar.checkManifest(this); 

    if (!regId.equals("")) { 
     sendIdToServer(regId); 
    } else { 
     GCMRegistrar.register(getApplicationContext(), Constants.SENDER_ID); 
    } 

    sendIdToServer(regId); 

} 

通过这些方法,发送GCM_regId服务器教程之一:

/** 
* GCM - sending the data in json format to server db 
* */ 
public void sendIdToServer(String regId) { 
    (new SendRegistrationIdTask(regId)).execute(); 
    GCM_regID = regId; 
} 

private class GCMReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String regId = intent 
       .getStringExtra(Constants.FIELD_REGISTRATION_ID); 
     token = regId; 

    } 
} 

private final class SendRegistrationIdTask extends 
     AsyncTask<String, Void, HttpResponse> { 
    // private String mRegId; 

    public SendRegistrationIdTask(String regId) { 
     // mRegId = regId; 
    } 

    @Override 
    protected HttpResponse doInBackground(String... regIds) { 
     // String url = Constants.SERVER_URL + "/register"; 

     return null; 
    } 

    @Override 
    protected void onPostExecute(HttpResponse response) { 
     if (response == null) { 

      return; 
     } 

     StatusLine httpStatus = response.getStatusLine(); 
     if (httpStatus.getStatusCode() != 200) { 
      Log.e(Constants.TAG, "Status: " + httpStatus.getStatusCode()); 
      return; 
     } 
    } 
} 

我不这么认为,需要GCMIntentService类这里是为了我的问题。请仔细研究并帮助我解决这个问题。

我可以在GCMIntentService类的onRegistered()中打印。这里是:

@Override 
protected void onRegistered(Context context, String regId) { 
    Intent intent = new Intent(Constants.ACTION_ON_REGISTERED); 
    intent.putExtra(Constants.FIELD_REGISTRATION_ID, regId); 
    context.sendBroadcast(intent); 
} 

我必须在onCreate()MainActivity上打印regId。

回答

5

注册设备需要一些时间..所以,如果您尝试在onCreate()中注册设备后立即检索注册ID,那么每次它将返回空值。因此,请尝试在设备内部注册onCreate()并在任何不同的activity/Service中检索id(您可以从GCMIntentService类中从GCM中检索api)。注意GCM Intent Service类的方法。

protected void onRegistered(Context arg0, String arg1) { 
    Logger.d(arg0, "REG ID="+arg1); 
    regID = arg1; 
} 

这是Reg完成后调用的方法。 因此,对于我的情况下,我把这个作为一个静态字符串,并正在访问它的REGID。 或者像你想要活动B上的REGID,最好在活动A上注册它,并通过上述方法中的静态字符串从GCMIntent类中检索它。

+0

我得到了GCMIntentService的值,所以如何在MainActivity中获取该值。我试过这样做,但没有为我工作。 – Anupam

+0

在GCMIntentService中,覆盖onRegistered()方法,并且您的值在String arg1中与上述参数类似。只需打印它。 – dominic

+1

把一个静态字符串等同于它并在ur类中访问该字符串。 – dominic

1

您第一次运行应用程序时没有获取REGID的原因是因为您的应用程序尚未具有REGID,因此需要向GCM服务器询问它。所以当您第一次拨打final String regId = GCMRegistrar.getRegistrationId(this);时,它会返回一个空字符串。但是,一旦您在下次打电话时(下次启动应用程序)您已注册GCM,您的regID将被退回。 GCM jar将regID作为字符串存储在应用程序首选项中。

当GCM服务器注册您的设备时,您将在调用GCMIntentService.OnRegistered()时了解它。在你的代码中,你正在广播中你的regID的意图,我假定你想用你的GCMReceiver(?)来处理,尽管在你已经显示的代码示例中,我没有看到你创建了一个类的实例或将它注册为一个接收器。

我认为你会想要在GCMReceiver.onReceive()是调用sendIdToServer()与您从意向获得的REGID。例如

@Override 
public void onReceive(Context context, Intent intent) { 
    String regId = intent 
      .getStringExtra(Constants.FIELD_REGISTRATION_ID); 
    Log.i("regID: ", regId); 
    sendIdToServer(regId); 
} 

我们有一个GCM/Airbop client sample,你可能想看看。它是基于您要应该来看看的GCM Demo app

0

如果您未在模拟器中登录Google帐户将无法正常工作。尝试使用Google(gmail)的有效帐户登录并查看。