2012-04-24 75 views
1

我希望我不會這樣做,但我一直在頭腦中過去兩天看起來很簡單。根據googlevogella的教程,我已經實現了我的C2DM應用程序客戶端+服務器。C2DM - 消息發送成功從服務器,但沒有出現在設備上

我會盡量簡要說明我的問題:

  1. 我的客戶端應用程序成功接收其registrationId
  2. 我的服務器成功收到其認證令牌
  3. 服務器使用其認證令牌和設備的registrationId發送消息。
  4. 當從服務器發送消息給客戶端時,我得到一個成功的消息響應代碼(例如id = 0:1335303367614556%fd55792500000030響應代碼:200,根據Google關於C2DM的文檔應該是成功的消息)。

所以我認爲最有可能我的問題是與我的接收器的消息,因爲他們從我的第三方服務器發送到C2DM服務器,但不能從那裏到我的應用程序。

我在StackOverflow上閱讀了有關設備端口的問題,但我目前使用相同的接收器來註冊應用程序和接收消息,並且註冊部分每次都工作。

這裏是我的接收器:

public class C2DRegistrationReceiver extends BroadcastReceiver{ 

@Override 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 

    if (action != null){ 
     if (action.equals("com.google.android.c2dm.intent.REGISTRATION")){ 
      // do something  
     } 
     else if (action.equals("com.google.android.c2dm.intent.RECEIVE")){ 
      // do something else 
     } 
    } 
} 
} 

我的清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="ro.raullepsa.coder" 
android:versionCode="1" 
android:versionName="1.0" > 

<!-- SDK min version --> 
<uses-sdk android:minSdkVersion="8" /> 


<!-- Only this application can receive the messages and registration result --> 
<permission android:name="ro.raullepsa.coder.permission.C2D_MESSAGE" android:protectionLevel="signature" /> 
<uses-permission android:name="ro.raullepsa.coder.permission.C2D_MESSAGE" /> 

<!-- This app has permission to register and receive messages from Google's c2dm --> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

<!-- Permission to use internet --> 
<uses-permission android:name="android.permission.INTERNET" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 

    <!-- Activities --> 
    <activity 
     android:name="ro.raullepsa.coder.activity.MainActivity" 
     android:label="@string/app_name" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

    <activity android:name="ro.raullepsa.coder.activity.RegistrationResultActivity" /> 
    <activity android:name="ro.raullepsa.coder.activity.MessageReceivedActivity" /> 
    <receiver 
     android:name="ro.raullepsa.coder.util.c2d.C2DRegistrationReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND"> 

     <!-- Receive messages --> 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="ro.raullepsa.coder" /> 
     </intent-filter> 

     <!-- Receive the registration id --> 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
      <category android:name="ro.raullepsa.coder" /> 
     </intent-filter> 
    </receiver> 
</application> 
</manifest> 

我沒有錯誤,我已經嘗試了一遍又一遍,以重新安裝應用程序,重新註冊,我已經嘗試了2個獨立的接收器,我已經手動強制停止了從C2DM運行並重新安裝的所有服務,但仍然沒有任何結果。

我試着在調試模式下等待接收器的第一行,它在註冊時會到達那裏,但從未從服務器發送消息之後。

我有點卡住,並會感謝任何幫助。如果需要,我可以提供更多的代碼,儘管我的應用程序基本上是Lars Vogel's tutorial。我錯過了什麼?

+0

您是否嘗試過另一臺設備上同步>後臺數據? – 2012-04-24 22:02:09

+0

實際上這是我的計劃,但我還沒有其他的Android設備支持這一點。如果我不能管理,我會借用朋友的設備並嘗試使用他的設備。 – 2012-04-24 22:03:58

+0

我認爲這是重要的一步,因爲它有助於縮小問題範圍並排除單個設備的問題。也嘗試在不同的網絡(3G,而不是無線) - 我看到C2DM消息無法到達某些網絡(雖然你可以註冊好,這可能不是問題)。 – 2012-04-24 22:24:27

回答

1

嗯,發生了一些有趣的事情。雖然我沒有對前一天晚上的代碼進行任何更改,但是當我今天早上醒來並打開Wifi時,我收到了一條消息。

現在一切正常。只要我發送消息,我就會自動進入設備。 (是的,我的Wifi也在昨晚)

我不確定是什麼原因導致了延遲,因爲我收到來自Google服務器的積極響應代碼,但沒有在我的設備上。可能需要一段時間才能發送第一條消息?我不確定。

我試過其他Wifi網絡,它也可以。希望我不會再遇到這個問題了。不過,我發現這種行爲有點奇怪。

+1

我對C2DM的經驗是自然界不可靠。它大部分時間都運行得非常好,但所有使用它的應用程序都應該設計爲處理未到達的消息 - 因此可能需要進行某種慢速背景調查 – 2012-04-25 11:22:24

1

GCM通知作爲背景數據發送到您的設備。

啓用後臺數據,這是

設置>帳戶和

相關問題