Android GCM:GCMRegistrar 提供空的注册 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11341305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:04:37  来源:igfitidea点击:

Android GCM : GCMRegistrar gives empty registration ID

androidgoogle-cloud-messaging

提问by techieWings

I have followed http://developer.android.com/guide/google/gcm/gs.html#server-appto implement GCM in my application

我已经按照http://developer.android.com/guide/google/gcm/gs.html#server-app在我的应用程序中实现 GCM

                    GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        if (GCMRegistrar.isRegistered(this)) {
            Log.d(TAG, GCMRegistrar.getRegistrationId(this));
        }
        final String regId = GCMRegistrar.getRegistrationId(this);

        if (regId.equals("")) {
            GCMRegistrar.register(this, sender_id);
            Log.d(TAG, "Registration id :  "+GCMRegistrar.getRegistrationId(this));
        }
            else {
            Log.d("info", "already registered as" + regId);
        }

that returns empty string as registration ID what else is needed to get the registration ID??

返回空字符串作为注册 ID 还需要什么来获取注册 ID?

采纳答案by techieWings

Creating a new project using google console with the same name as application's name in eclipse helped me to solve this problem.

在 Eclipse 中使用与应用程序名称相同的名称使用谷歌控制台创建一个新项目帮助我解决了这个问题。

回答by Mani

Empty String comes when device is not registered successfully. There may be following reasons for it-

设备未成功注册时出现空字符串。可能有以下原因——

  1. Put you GCM code in application package. [You can make another package with same name of application package]
  2. Put all permissions properly.
  1. 将您的 GCM 代码放在应用程序包中。[您可以制作另一个与应用程序包名称相同的包]
  2. 正确放置所有权限。

回答by Nick Wong

I also got the empty registration ID and finally fix it. Here are my solutions:

我也得到了空的注册ID,最后修复了它。以下是我的解决方案:

1) check the project ID. It should be the 12 char long shown in the hyperlink of Google API's console (not the Project ID you named in the project summary)
2) try change the final String regIdto String regId. I don't know why but it works for me.

1)检查项目ID。它应该是 Google API 控制台超链接中显示的 12 个字符长(不是您在项目摘要中命名的项目 ID)
2) 尝试将最终的 String regId更改为String regId。我不知道为什么,但它对我有用。

Hope it helps.

希望能帮助到你。

回答by Giuseppe

    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    final String regId = GCMRegistrar.getRegistrationId(this);

    if (regId.equals("")) {
        GCMRegistrar.register(this, "YOUR_ACCOUNT");
    } else {
        app.prefs.edit().putString("prefs_googleid", regId).commit();
        GCMRegistrar.setRegisteredOnServer(this, true);
        Log.v(TAG, "Already registered");
    }

then wait the call back in your receiver that extend GCMBaseIntentService in the onRegistered ovverride, there you will get your ID registration.

然后等待在 onRegistered ovverride 中扩展 GCMBaseIntentService 的接收器中的回调,在那里您将获得您的 ID 注册。

Anyway I full create an app that use GCM following this post: http://developer.android.com/guide/google/gcm/gs.html

无论如何,我在这篇文章之后完全创建了一个使用 GCM 的应用程序:http: //developer.android.com/guide/google/gcm/gs.html

回答by Zakharov Roman

I got the same issue, the solution was:

我遇到了同样的问题,解决方案是:

You need to implement a GCMIntentService(extending from GCMBaseIntentService). And DO NOT !!!rename GCMIntentServiceby something else, this was a reason in my case.

您需要实现一个GCMIntentService(从 GCMBaseIntentService 扩展)。和DO NOT!用别的东西重命名GCMIntentService,这是我的一个原因。

回答by Ethan

Make sure you defined the service in the "app_package" as this intent service will be called by the GCMBroadcastReceiver

确保您在“app_package”中定义了该服务,因为此 Intent 服务将由 GCMBroadcastReceiver 调用

For example

例如

<service android:name=".GCMIntentService" />

or

或者

<service android:name="app_package.GCMIntentService" />

Fail to define the service correctly, there will be no callback for onRegister, and your Register Id is always empty

没有正确定义服务,onRegister不会有回调,你的Register Id一直为空

回答by qgicup

I had the same problem with my application. First thing that was wrong, was with my GCMIntentService. It has to be at the root of your package.

我的应用程序遇到了同样的问题。错误的第一件事是我的 GCMIntentService。它必须在你的包的根目录下。

I will include here a short snippet of my GCMIntentService. Please keep in mind that, you will receive your registrationID on GCMIntentService register method

我将在此处包含我的 GCMIntentService 的一小段。请记住,您将在 GCMIntentService 注册方法上收到您的注册 ID

public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super(CloudMessagingUtility.SENDER_ID);
}

/**
 * Method called on device registered
 **/
@Override
protected void onRegistered(Context context, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);

//here call your methods to send the registrationId to your webserver CloudMessagingUtility.register(context, registrationId); }

//这里调用您的方法将注册ID发送到您的网络服务器 CloudMessagingUtility.register(context, registrationId); }

/**
 * Method called on device un registred
 * */
@Override
protected void onUnregistered(Context context, String registrationId) {
    Log.i(TAG, "Device will unregister from Google Cloud Messaging");
    //unregister the device from your own webserver too
    CloudMessagingUtility.unregister(context, registrationId);
}

/**
 * Method called on Receiving a new message
 * */
@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message from GCM:");
    String message = "";//TODO handle here the messages!

}

/**
 * Method called on receiving a deleted message
 * */
@Override
protected void onDeletedMessages(Context context, int total) {
    Log.i(TAG, "Received deleted messages notification");
    String message = getString(R.string.gcm_deleted, total);
    displayMessage(context, message);

}

/**
 * Method called on Error
 * */
@Override
public void onError(Context context, String errorId) {
    Log.e(TAG, "Received error: " + errorId);
}

}

}

you have to make the right configuration in the Android

你必须在Android中进行正确的配置

e <service android:name=".GCMIntentService" /><receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >

        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name=" your package here" />
        </intent-filter>
    </receiver>

回答by dionkta

GCMRegistrar.register(this, sender_id); Log.d(TAG, "Registration id : "+GCMRegistrar.getRegistrationId(this));

GCMRegistrar.register(this, sender_id); Log.d(TAG, "注册id : "+GCMRegistrar.getRegistrationId(this));

Calling getRegistrationId() straight after register() is risky because register might not return the id straight away.

在 register() 之后直接调用 getRegistrationId() 是有风险的,因为 register 可能不会立即返回 id。

For my case I did a check later on in my script (upon button press) using:

对于我的情况,我稍后在我的脚本中(按下按钮时)使用以下方法进行了检查:

boolean registered = GCMRegistrar.isRegistered(this);

布尔注册 = GCMRegistrar.isRegistered(this);

if registered then call GCMRegistrar.getRegistrationId(this)); to get the ID registered.

如果已注册,则调用 GCMRegistrar.getRegistrationId(this)); 获取注册ID。

回答by Mohammed_AlRawhani

I have tried the second solution of Nick Wong and i changed => public static final String PROPERTY_REG_ID = "registration_id";to => public static String PROPERTY_REG_ID = "registration_id";

我已经尝试了 Nick Wong 的第二个解决方案,我把 => public static final String PROPERTY_REG_ID = "registration_id";改为 => public static String PROPERTY_REG_ID = "registration_id";

and simply it worked.

很简单,它奏效了。

回答by readerind

Since GCMRegistrar.getRegistrationId is asynchronous, I added a counter to check whether regId is actually empty or not. If empty the counter would just increment and try to get the registration Id again. A fairly large number like 100 should suffice.

由于 GCMRegistrar.getRegistrationId 是异步的,我添加了一个计数器来检查 regId 是否实际上为空。如果为空,计数器只会增加并尝试再次获取注册 ID。像 100 这样相当大的数字就足够了。

int counter = 0;
                while (true) {
                    regID = GCMRegistrar.getRegistrationId(this);
                    counter++;
                    if (counter == 100 || regID != null || !regID.equals("")) {
                        break;
                    }
                }