我无法从 Android GCM 获取注册 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11713363/
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 08:33:11  来源:igfitidea点击:

I can not get registration ID from Android GCM

androidgoogle-cloud-messaging

提问by Anil Kocabiyik

Although I try other answer about this problem, it can't be solved. Sender ID is correct, permissions are added, API key is true.

尽管我尝试了有关此问题的其他答案,但无法解决。发件人 ID 正确,添加权限,API 密钥为 true。

I use this post for creating the project: http://developer.android.com/guide/google/gcm/gs.html#server-app

我使用这篇文章来创建项目:http: //developer.android.com/guide/google/gcm/gs.html#server-app

    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
      GCMRegistrar.register(this, SENDER_ID);
      regId = GCMRegistrar.getRegistrationId(this);
    } else {
      Log.v(TAG, "Already registered");
    }
    String did = getDeviceID();

it returns empty string.

它返回空字符串。

回答by Amir Uval

Do you have GCMIntentServicedefined correctlyat the root package of your app?

是否在应用程序的根包中正确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="my_app_package" />
  </intent-filter>
</receiver>

Make sure that "my_app_package"is identical to the main package of your app, or your id will return an empty string.

确保它"my_app_package"与您的应用程序的主包相同,否则您的 id 将返回一个空字符串。

Also notice that

还要注意的是

    GCMRegistrar.register(this, "...");

is asynchronous. Therefore, calling GCMRegistrar.getRegistrationId(this)immediately after that is not reliable, so you should avoid it.

是异步的。因此,GCMRegistrar.getRegistrationId(this)在那之后立即调用是不可靠的,因此您应该避免它。

The id will arrive via broadcast callback to your GCMIntentService, as a part of the registration procedure. from there you can then store the gcm/fcm key anywhere you like, and use it in other parts of the application (usually on the server).

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

回答by EvanBlack

If your application launches first time, you have to wait for OnRegistered callback on your GCMIntentService.java file.

如果您的应用程序第一次启动,您必须等待 GCMIntentService.java 文件上的 OnRegistered 回调。

GCMRegistrar.register(this, SENDER_ID);
// Android does NOT wait for registration ends and executes immediately line below
// So your regId will be empty.
regId = GCMRegistrar.getRegistrationId(this);

GCMIntentService.java:

GCMIntentService.java:

@Override
protected void onRegistered(Context c, String regId) {
    // You can get it here!
}

Edit: Newer version of GCM library (which bundled with google play services library) waits for response and returns the Registration ID. So this answer is for older GCM libraries.

编辑:较新版本的 GCM 库(与 google play 服务库捆绑在一起)等待响应并返回注册 ID。所以这个答案适用于较旧的 GCM 库。

回答by Dhaval Jivani

I have same problem after 1 day struggle I find solution. First i Put my GCM Related Code in To My Main Package and make change according to My Package in androidManifest.xml

经过 1 天的斗争,我遇到了同样的问题,我找到了解决方案。首先,我将我的 GCM 相关代码放入我的主包中,并根据 androidManifest.xml 中的我的包进行更改

i give you simple example. My project name is GCMExample and i have Three package 1. com.examle.GCMExample(Main Package) , 2. com.examle.GCMExample.activity(Second Package) , 3. com.example.GCMExample.adapter(Third Package)

我给你简单的例子。我的项目名称是 GCMExample,我有三个包 1. com.examle.GCMExample(Main Package) 2. com.examle.GCMExample.activity(Second Package) 3. com.example.GCMExample.adapter(Third Package)

I am not Getting Registration Id When My GCM Related Class File Into My Second Package

当我的 GCM 相关类文件进入我的第二个包时,我没有获得注册 ID

My GCM Related class like 1. GCMIntentService , 2. ServerUtilities , 3. CommonUtilities 4. WakeLocker put Into My Main Package com.example.GCMExample

我的 GCM 相关类,如 1. GCMIntentService 、 2. ServerUtilities 、 3. CommonUtilities 4. WakeLocker 放入我的主包 com.example.GCMExample

also my androidManifest.xml

还有我的 androidManifest.xml

     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />
     <uses-permission android:name="android.permission.WAKE_LOCK" />
     <uses-permission android:name="android.permission.VIBRATE" />

     <permission android:name="com.example.GCMExample.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

     <uses-permission android:name="com.example.GCMExample.C2D_MESSAGE" />

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
        <!-- Receives the actual messages. -->
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <!-- Receives the registration id. -->
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

        <category android:name="com.example.GCMExample" />
      </intent-filter>
</receiver>  

<service android:name="com.example.GCMExample.GCMIntentService" />

回答by divanshu

Registering app com.example.ilkgcm of senders "my_sender_id" -- This error suggests you haven't changed your 'my_sender_id' to a valid sender id which would be a 12-letter code.

注册发件人“my_sender_id”的应用 com.example.ilkgcm -- 此错误表明您尚未将“my_sender_id”更改为有效的发件人 ID,该 ID 为 12 个字母的代码。

回答by Vinoth Prakash K

If you are not getting response from the registration.one of the main reason is your manifest file is not configured correctly...especially give the "package_name"(your app package name like com.xxx.yyy) in the and correctly.

如果您没有从注册中得到响应。主要原因之一是您的清单文件配置不正确...尤其是正确地提供“package_name”(您的应用程序包名称,如 com.xxx.yyy)。

回答by Twinsens

For example your receiver class name is : "MyCustomIntentService.java" , just change this name as "GCMIntentService.java" then try it again. This is simple solution. Just change your file name on the SRC area.

例如您的接收器类名称是: "MyCustomIntentService.java" ,只需将此名称更改为 "GCMIntentService.java" 然后再试一次。这是简单的解决方案。只需在 SRC 区域更改您的文件名。