2013-08-06 46 views
2

我一直在努力爲我的應用程序獲取設備ID。這裏是我的代碼GCMRegistrar.getRegistrationId(上下文)無法獲取設備ID

此代碼是從我的類

GCMRegistrar.checkDevice(this); 
GCMRegistrar.checkManifest(this); 
device_key = GCMRegistrar.getRegistrationId(this); 

此代碼是從清單文件

<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="com.k2b.kluebook" /> 
     </intent-filter> 
    </receiver> 
    <service android:name=".GCMIntentService" /> 

我在主包寫GCMIntentService類。

public class GCMIntentService extends GCMBaseIntentService { 

private static final String TAG = "GCMIntentService"; 

public GCMIntentService() { 
    super(SENDER_ID); 
} 

/** 
* Method called on device registered 
**/ 
@Override 
protected void onRegistered(Context context, String registrationId) { 
    Log.i(TAG, "Device registered: regId = " + registrationId); 
    displayMessage(context, "Your device registred with GCM"); 
    Log.d("NAME", "gcm"); 
    ServerUtilities.register(context, "gcm", "gcm", registrationId); 
} 

/** 
* Method called on device un registred 
* */ 
@Override 
protected void onUnregistered(Context context, String registrationId) { 
    Log.i(TAG, "Device unregistered"); 
    displayMessage(context, getString(R.string.gcm_unregistered)); 
    ServerUtilities.unregister(context, registrationId); 
} 

/** 
* Method called on Receiving a new message 
* */ 
@Override 
protected void onMessage(Context context, Intent intent) { 
    Log.i(TAG, "Received message"); 
    String message = intent.getExtras().getString("mes"); 
    displayMessage(context, message); 
    // notifies user 
    generateNotification(context, message); 
} 

/** 
* 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); 
    String key = "failed"; 
    displayMessage(context, message); 
    // notifies user 
    generateNotification(context, message); 
} 

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

@Override 
protected boolean onRecoverableError(Context context, String errorId) { 
    // log message 
    Log.i(TAG, "Received recoverable error: " + errorId); 
    displayMessage(context, 
      getString(R.string.gcm_recoverable_error, errorId)); 
    return super.onRecoverableError(context, errorId); 
} 

/** 
* Issues a notification to inform the user that server has sent a message. 
*/ 
private static void generateNotification(Context context, String message) { 
    int icon = R.drawable.ic_launcher; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 
    String title = context.getString(R.string.app_name); 
    Intent notificationIntent = new Intent(context, 
      FragmentOpenActivity.class); 
    notificationIntent.putExtra(key, key); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = PendingIntent.getActivity(context, 0, 
      notificationIntent, 0); 
    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.defaults |= Notification.DEFAULT_SOUND; 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify(0, notification); 

} 

}

這麼多的搜索是通過我做的一切,但我不能找到這個解決方案,我想很多開發商都具有相同的問題,但我不能找到我的這個錯誤。請幫我找到解決辦法

+0

LogCat中有任何錯誤嗎?您是否嘗試使用多個不同的設備? –

+0

我在模擬器,bluestack和android 4.1中也嘗試過。 –

+0

你有什麼在你的logCat? –

回答

1

好吧,讓我把我在這裏做的:

  1. 首先,你必須創建項目在谷歌和得到SENDER_ID。
  2. 然後,我有這樣的方法來registerGCM並調用它的OnCreate:

private void registerGCM() { 
     // Make sure the device has the proper dependencies. 
     GCMRegistrar.checkDevice(getApplicationContext()); 

     // Make sure the manifest was properly set - comment out this line 
     // while developing the app, then uncomment it when it's ready. 
     GCMRegistrar.checkManifest(getApplicationContext()); 
     GCMRegistrar.register(getApplicationContext(), GCMIntentService.SENDER_ID); 
     if (GCMRegistrar.isRegistered(getApplicationContext())) { 
      String registrationCGMId = GCMRegistrar.getRegistrationId(getApplicationContext()); 
     }else{ 
      GCMRegistrar.register(getApplicationContext(), GCMIntentService.SENDER_ID); 
     } 
    } 

如果錯誤仍然存​​在,檢查谷歌Play應用程序(如果它正常工作),互聯網連接或手機時鐘時間。

+0

我試過調試模式,這很有趣。第一次它跳過if條件並執行else語句,然後如果我回來然後轉到另一個活動,它會執行if條件並在註冊後顯示設備ID –

+0

我只能在設備ID之前獲得設備ID得到它 –

+1

當然,因爲註冊是在後臺完成的。所以如果你想強迫設備ID在同一個活動中,你將不得不檢查你是否已經有設備ID。在我的應用程序中,我有一個需要設備ID的表單,所以我必須驗證字段,看看我是否已經有設備ID ..如果我沒有,我再次調用該方法來重新註冊,直到有ID 。 –