2012-12-24 211 views
1

我正在開發一個應用程序,我必須使用GCM註冊我的設備,但它沒有註冊我的設備。我已授予的所有權限在menifest 我日誌中的應用程序是在這裏Android的GCM註冊設備

方法來註冊onGCM是

public void register() 
     { 
      GCMRegistrar.checkDevice(this); 
      GCMRegistrar.checkManifest(this); 

      if (GCMRegistrar.isRegistered(this)) 
       { 
        Log.d("info", GCMRegistrar.getRegistrationId(this)); 
       } 

      final String regId = GCMRegistrar.getRegistrationId(this); 
      System.out.println("registration ID is " + regId); 
      if (regId.equals("")) 
       { 
        System.out.println("registration ID is " + regId); 
        GCMRegistrar.register(this, CommonUtilities.SENDER_ID); 
        Log.d("info", GCMRegistrar.getRegistrationId(this)); 
        System.out.println("Get Reg ID: " + GCMRegistrar.getRegistrationId(this)); 
       } 
      else 
       { 
        Log.d("info", "already registered as" + regId); 
       } 
     } 

和我GCM服務

public class GCMService extends GCMBaseIntentService 
{ 
    public GCMService() 
     { 

      super(CommonUtilities.SENDER_ID); 
     } 

    public void generateNotification(Context context, String message) 
     { 
      long when = System.currentTimeMillis(); 
      NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notification = new Notification(R.drawable.ic_launcher, message, when); 
      String title = context.getString(R.string.app_name); 

      Intent notificationIntent = new Intent(context, OffersActivity.class); 
      PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); 
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
      notification.setLatestEventInfo(context, title, message, intent); 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 
      notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS; 
      notificationManager.notify(0, notification); 
     } 

    @Override 
    protected void onError(Context arg0, String arg1) 
     { 
     } 

    @Override 
    protected void onMessage(Context arg0, Intent arg1) 
     { 
      Log.d("GCM", "RECIEVED A MESSAGE");    
      generateNotification(arg0, "New Offers Are Available"); 
     } 

    @Override 
    protected void onRegistered(Context arg0, String arg1) 
     { 
     } 

    @Override 
    protected void onUnregistered(Context arg0, String arg1) 
     { 
     } 
} 

和我menifest是

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<uses-permission android:name="android.permission.INTERNET" /> 
<!-- Map Location permissions --> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<!-- Notification permissions --> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> 
<uses-permission android:name="android.permission.READ_OWNER_DATA" />  

<permission 
    android:name="myPackage.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 

<!-- Storage permissions --> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

<!-- Internet permissions --> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

<!-- Call Phone permissions --> 
<uses-permission android:name="android.permission.CALL_PHONE" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="cmyPackage.SplashScreen" 
     android:label="AppName" 
     android:theme="@android:style/Theme.NoTitleBar" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="myPackage.CatagoriesActivity" 
     android:configChanges="keyboardHidden|orientation|keyboard" 
     android:theme="@android:style/Theme.NoTitleBar" > 
    </activity> 
    <activity 
     android:name="myPackage.OffersActivity" 
     android:configChanges="keyboardHidden|orientation|keyboard" 
     android:theme="@android:style/Theme.NoTitleBar" > 
    </activity> 
    <activity 
     android:name="myPackage.SettingsActivity" 
     android:configChanges="keyboardHidden|orientation|keyboard" 
     android:theme="@android:style/Theme.NoTitleBar" > 
    </activity> 
    <activity 
     android:name="myPackage.OfferDetailsAtivity" 
     android:configChanges="keyboardHidden|orientation|keyboard" 
     android:theme="@android:style/Theme.NoTitleBar" > 
    </activity> 
    <activity 
     android:name="myPackage.DealInfoActivity" 
     android:configChanges="keyboardHidden|orientation|keyboard" 
     android:theme="@android:style/Theme.NoTitleBar" > 
    </activity> 
    <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="myPackage" /> 
     </intent-filter> 
    </receiver> 
    <service android:name=".GCMService" />   

    <uses-library android:name="com.google.android.maps" /> 
</application>  


I/System.out(17369): Registering 
D/GCMRegistrar(17369): resetting backoff for packageName 
V/GCMRegistrar(17369): Registering app packageName of sendersID xxxxxxxxxxxxxx 
registration id  Still Empty 
(17369): Not yet registered 

我有多個包在我的應用程序

public final class CommonUtilities 
{ 
    public static final String SENDER_ID = "xxxxxxxxxxxxx"; 
    static final String TAG = "AndroidHive GCM"; 

    public static final String DISPLAY_MESSAGE_ACTION = "myPackage.pushNotifications.DISPLAY_MESSAGE"; 
    static final String EXTRA_MESSAGE = "message"; 
    static void displayMessage(Context context, String message) 
     { 
      Intent intent = new Intent(DISPLAY_MESSAGE_ACTION); 
      intent.putExtra(EXTRA_MESSAGE, message); 
      context.sendBroadcast(intent); 
     } 
} 
+1

您的模擬器(或真實手機)是Google API的嗎?你有沒有檢查http://stackoverflow.com/questions/11341305/android-gcm-gcmregistrar-gives-empty-registration-id? – sabadow

+0

我正在測試真實設備2.2 Froyo 7英寸平板電腦三星 – Farrukh

+0

@ Farrukh:你解決了這個問題,我面臨同樣的問題。請幫助。 –

回答

1

的你應該包括你的日誌輸出,讓我們知道,當你運行你的應用程序會發生什麼。您還應該看看GCM演示並將其用作基礎,因爲您似乎缺少一些重要的代碼:http://code.google.com/p/gcm/source/browse/samples/gcm-demo-client/src/com/google/android/gcm/demo/app/DemoActivity.java

如果您從GCM獲得您的註冊ID,那麼您已註冊GCM(您是否獲得了註冊ID?),下一步是註冊您的服務器。這應該在以下如果塊的其他發生:

if (regId.equals("")) 

而在onRegistered在GCM服務也是如此。這是當您的應用程序成功註冊到GCM時將觸發的回調。嘗試註冊後立即獲取註冊ID將不會輸出任何內容,因爲註冊GCM是異步的。

0

您必須輸入您UNIQUE_ID(即12個字符)CommonUtilities.java

/** 
* Google API project id registered to use GCM. 
*/ 
static final String SENDER_ID = 123456789012; <---- 
+0

我已經發布了我的常用工具類,並在那裏我有唯一的發件人ID – Farrukh

+0

我的ID是13個字符 – Farrukh

1

如果尚未解決。我想這可能是問題的原因......添加此

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

到Android清單XML。

希望它有幫助。