GCMRegistrar.getRegistrationId(this)始终返回空字符串(使用GCM的Android推送通知)

时间:2013-12-23 10:24:26

标签: android google-cloud-messaging

 public void registerClient()
 {
  try
  {
   // Check that the device supports GCM (should be in a try / catch)
   GCMRegistrar.checkDevice(this);

   // Check the manifest to be sure this app has all the required
   // permissions.
   GCMRegistrar.checkManifest(this);

   // Get the existing registration id, if it exists.
   regId = GCMRegistrar.getRegistrationId(this);

   if (regId.equals(""))
   {

    // register this device for this project
    GCMRegistrar.register(this, PROJECT_ID);
    regId = GCMRegistrar.getRegistrationId(this);

regId = GCMRegistrar.getRegistrationId(this);总是返回一个空字符串。是否已弃用GCMRegister类? 我从sdk / extras / google / gcm / gcm-client路径添加了gcm jar。 我也尝试添加谷歌播放服务库并执行代码(删除gcm.jar)但它然后说GCMRegistrar类未找到异常 我检查了所有权限,他们似乎是对的。 这是我的清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gcmclient"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <!-- receives GCM messages -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- GCM connects to Google services -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- GCM requires a Google account -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.READ_OWNER_DATA" />

    <!-- wake the processor if a GCM message is received -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="com.example.gcmclient" />

                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

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

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

</manifest>

3 个答案:

答案 0 :(得分:1)

您的代码看起来没问题,但是当您使用GCM时,您必须从设置中同步帐户。按照步骤。

转到设置---&gt;添加帐户---&gt;谷歌

答案 1 :(得分:0)

在我看来,在推出GCM后,他们过早地弃用了旧方法。

但是,您似乎正在混合旧方法和新Play服务实现的代码。建议您最好以Play Services example GCM client为起点重新开始

答案 2 :(得分:0)

我使用GCM请在调用GCM注册方法之前进行这些检查:

Android 4.0.4必须使用正确同步的Google帐户才能向GCM注册,以便您可以编写类似的内容

 private boolean isGoogleAccountRequired=true;
 private Account[] accounts;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.sample);

   String firmwareVersion=Build.VERSION.RELEASE;
   String firmwareVersionsStrings[]=firmwareVersion.split("\\.");

   int firstDigit=Integer.valueOf(firmwareVersionsStrings[0]);
   int secondDigit=Integer.valueOf(firmwareVersionsStrings[1]);
   int thirdDigit=0;

   if(firmwareVersionsStrings.length>=3){
    thirdDigit=Integer.valueOf(firmwareVersionsStrings[2]);
   }

   if(firstDigit>=4){
  if(secondDigit>0){
    isGoogleAccountRequired=false;
  }else if(secondDigit==0){
    if(thirdDigit>=4){
       isGoogleAccountRequired=false;
    }
  }
   }


   if(isGoogleAccountRequired && !GCMRegistrar.isRegistered(this)){
    boolean isGoogleAccountPresent=isGoogleAccountPresent(this);

        // Check whether Google Account Present or not if not act accordingly
        if(! isGoogleAccountPresent){
    // Show Dialog to add google account and sync it
        }else{

           //Check if the account added is synced or not if not sync it programatically
           boolean syncEnabled=false;
           if(accounts.length>0){
             syncEnabled = ContentResolver.getSyncAutomatically(accounts[0], ContactsContract.AUTHORITY);   
       }

           if(!syncEnabled){
            ContentResolver.setSyncAutomatically(accounts[0], ContactsContract.AUTHORITY, true);
       }
             GCMRegistrar.register(YourActivity.this,SENDER_ID);

        }
   }else if(!GCMRegistrar.isRegistered(this)){
        GCMRegistrar.register(YourActivity.this,SENDER_ID);

   }
 }

现在在GCMIntentService中覆盖这些方法:

   @Override
   protected void onRegistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
   Log.i(TAG, "device is registered to GCM server with Reg ID In GCMINTENT::::"+ arg1);
   }

   protected void onMessage(Context context, Intent intent) {

   }

   @Override
   public void onError(Context context, String errorId) {
   Log.i(TAG, "Received error: " + errorId);
   }

如果您仍未获得Reg Id,请检查onError消息中的日志错误ID。

为写入同步设置添加此权限:

    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>