2012-04-23 39 views
3

好吧,我真的不知道我在這裏缺少什麼。我試圖讓C2DM爲我們的應用工作,特別是處理廣播讓我很掙扎。C2DM inapp註冊:無法啓動服務意圖

我們有一個appwide廣播接收器:

public final class AppBroadcastReceiver extends BroadcastReceiver { 

    //get an instance if not already present 
    public static AppBroadcastReceiver getInstance(final IntentFilter filter) { 
     if (instance == null) { 
      instance = new GlobalBroadcastReceiver(); 
     } 
     if (filter != null) { 
      filter.addAction("com.google.android.c2dm.intent.REGISTRATION"; 
      filter.addAction("com.google.android.c2dm.intent.RECEIVE"); 
      filter.addCategory("my.package.name"); 
     } 
     return instance; 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     final String broadcastAction = intent.getAction(); 
     Log.d(logTag, String.format("GlobalBroadcastReceiver::onReceive for action = %s", broadcastAction)); 

     if ("com.google.android.c2dm.intent.REGISTRATION".equals(broadcastAction)) { 
      for (final AppBroadcastListener l : listeners) { 
       l.c2dmRegistration(intent); 
      } 
     } else if ("com.google.android.c2dm.intent.RECEIVE".equals(broadcastAction)) { 
      for (final ApplBroadcastListener l : listeners) { 
       l.c2dmReceive(intent); 
      } 
     }//else 
    }//onReceive 
} 

AppBroadcastListener是我們的所有活動都執行,以確保適當的方法的接口都至少存在。在他們的onResume(),onStop()方法中,活動分別在Receiver註冊和取消註冊。

出於測試目的,我有一個調試的活動,證明我以下兩種方法:

public void sendC2DM(View v){ 
    Intent intent= new Intent(); 
    intent.setAction(com.google.android.c2dm.intent.RECEIVE); 
    intent.putExtra("message","Bender: \"kiss my shiny metal ass!\""); 
    intent.addCategory(getPackageName()); 

    sendBroadcast(intent); 
} 

public void registerC2DM(View v){ 
    Intent registrationIntent = new Intent(com.google.android.c2dm.intent.REGISTRATION); 
    registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate 
    registrationIntent.putExtra("sender", [email protected]); 
    startService(registrationIntent); 
} 

而在android.manifest我已經添加在<application> -tag的follwoing線:

<receiver 
     android:name=".AppBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" > 

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

      <category android:name="my.package.name" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

      <category android:name="my.package.name" /> 
     </intent-filter> 
    </receiver> 

因此,我們希望接收廣播的每個活動都是在BroadcastResceiver onResume()上註冊自己,當發生某些事情時,BroadcastReceiver將捕獲並調用已實現的方法。例如。 ,記錄消息或顯示吐司。

但是,當我發送一個「C2DM消息」,我看到這個結構正在爲自制廣播工作。 (本德的消息在吐司彈出),但registerC2DM().startService(registrationIntent);只是記錄:

無法啓動服務意向{ 行爲= com.google.android.c2dm.intent.REGISTRATION(有演員)}:未發現

我不知道我在這裏錯過了什麼。一般建議似乎是:檢查您的android.manifest(完成)或:使用註冊的Gmail帳戶登錄。
不知道這回合。我用我的Gmail帳戶登錄是的,但不是與我們在註冊時輸入的[email protected]。我也堅信這不可能是解決方案。 (告訴我們所有的客戶使用這個帳戶登錄...... ehm no?!)。
所以我想這是別的東西,但我無法找到它:C

回答

3

OK,這是真正的意思現貨:

如果你想爲C2DM註冊使用

com.google.android.c2dm.intent.REGISTER 

但搭上回答這個消息,你必須設置你廣播接收機聽

com.google.android.c2dm.intent.REGISTRATION 

微妙?是的,在這裏再次錯誤並修正版本:

//false 
public void registerC2DM(View v){ 
    Intent registrationIntent = new Intent(com.google.android.c2dm.intent.REGISTRATION); 
    registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate 
    registrationIntent.putExtra("sender", [email protected]); 
    startService(registrationIntent); 
} 


//true 
public void registerC2DM(View v){ 
    Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); 
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate 
registrationIntent.putExtra("sender", [email protected]); 
    startService(registrationIntent); 
} 

被砸實現這一前3個鍵盤... :)