2013-12-23 24 views
1
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(本);總是返回一個空字符串。 GCMRegister類是否被棄用? 我已經從sdk/extras/google/gcm/gcm-client路徑添加了gcm jar。 我也嘗試添加谷歌播放服務庫和執行代碼(刪除gcm.jar),但它然後說GCMRegistrar類沒有發現異常 我已檢查所有權限,他們似乎是正確的。 這裏是我的清單文件GCMRegistrar.getRegistrationId(本)總是返回空字符串(使用GCM的Android推送通知)

<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> 
+0

是的,它是過時了,看到http://developer.android.com/reference/com/google/android/gcm/GCMRegistrar.html – NickT

回答

1

您的代碼看起來像確定,但是當您使用GCM時,您必須從設置中同步帳戶。按照步驟操作。

進入設置 - >添加帳戶--->谷歌

+0

這真的工作....謝謝 – user2291144

0

他們棄用了在我看來,引進後做GCM太快的老方法。

但是,您似乎混淆了舊方法和新Play Services實現的代碼。您最好建議以Play Services example GCM client作爲起點再次開始

0

我使用的是GCM請把這些檢查調用GCM註冊方法之前:

一個正確同步谷歌帳戶是強制性的Android 4.0.4與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); 
    } 

如果您仍未收到註冊ID,請檢查onError消息中的日誌錯誤ID。

添加此權限編寫同步設置:

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