应用注册ID未在GCM Android中生成

the*_*der 3 notifications android registration google-cloud-messaging

我已经尝试了一些论坛页面并通过stackoverflow搜索同样的事情,但我没有找到解决方案.

我使用以下代码生成GCM通知的注册ID.但我得到一个空字符串作为注册ID:

GCMRegistrar.checkDevice(本); GCMRegistrar.checkManifest(本);

if(GCMRegistrar.isRegistered(this)){Log.d("info",GCMRegistrar.getRegistrationId(this)); }

regId = GCMRegistrar.getRegistrationId(this);

Log.v("log", " register id oslo : " + regId);

if (regId.equals("")) {
    // replace this with the project ID
    GCMRegistrar.register(this, "718437041388");
    Log.d("info", GCMRegistrar.getRegistrationId(this));
} else {
    Log.d("info", "already registered as" + regId);
}
Run Code Online (Sandbox Code Playgroud)

请帮帮我,告诉我,如果我有什么不对劲.

先谢谢你,Nirav.

jjL*_*Lin 7

您不应该在之后获得registrationId GCMRegistrar.register(this, "718437041388");
事实上,您是否有正确的GCMIntentService.java接收regId?
如果是,您可以从onRegistered内部获得regIdGCMIntentService.java

 @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        Log.d("GCMIntentService", "in GCMIntentService");
        displayMessage(context, getString(R.string.gcm_registered));
        ServerUtilities.register(context, registrationId);
    }
Run Code Online (Sandbox Code Playgroud)


Ami*_*val 5

请注意

    GCMRegistrar.register(this, "...");
Run Code Online (Sandbox Code Playgroud)

是异步的.因此,GCMRegistrar.getRegistrationId(this)在此之后立即呼叫是不可靠的,所以你应该避免它.

GCMIntentService作为注册程序的一部分,id将通过广播回调到达您的身份.从那里你可以将gcm密钥存储在你喜欢的任何地方,并在应用程序的其他部分使用它(通常在服务器上).

请注意,GCMIntentService应该在您的应用程序的根包中定义它才能工作.