2013-04-20 146 views
3

我已經按照google關於啓動gcm服務器和客戶端的說明。 我的問題是,雖然我從服務器獲取成功的GCM:無法在使用GCM的設備上接收消息

MulticastResult(multicast_id = 8287827393174436535,總= 1,成功= 1,失敗= 0,canonical_ids = 0,結果: [MESSAGEID = 0: 1366468335181772%8e1522ac00000031]

我不能看到接收客戶端的消息的任何移動 - 的onMessage()不叫 客戶端ID以及服務器ID是正確的,據我。可以告訴。

我會很感激任何幫助...... :)

服務器端代碼:

try { 

Sender sender = new Sender(API_KEY); 
Message message = new Message.Builder().collapseKey("1") 
     .timeToLive(3) 
     .delayWhileIdle(true) 
     .addData("message", 
       "this text will be seen in notification bar!!").build(); 

ArrayList<String> devices = new ArrayList<String>(); 

devices.add(DEVICE_ID1); 
//devices.add(DEVICE_ID2); 

MulticastResult result = sender.send(message, devices, 2); 
//Result result = sender.send(message, DEVICE_ID1, 2); 
System.out.println(result.toString()); 
if (result.getResults() != null) { 
    int canonicalRegId = result.getCanonicalIds(); 
    if (canonicalRegId != 0) { 
    } 
} else { 
    int error = result.getFailure(); 
    System.out.println(error); 
} 
} catch (Exception e) { 
    // TODO: handle exception 
} 

客戶機代碼: 登記:

GCMRegistrar.checkDevice(this); 
     GCMRegistrar.checkManifest(this); 
     final String regId = GCMRegistrar.getRegistrationId(this); 
     if (regId.equals("")) { 
      GCMRegistrar.register(this, SENDER_ID); // Note: get the sender id from configuration. 
      String regIdString = GCMRegistrar.getRegistrationId(this); 
      Log.v(TAG, "RegisterId, regId: " + regIdString); 
     } else { 

      Log.v(TAG, "Already registered, regId: " + regId); 
     } 

和gcmIntentServiceCreated(部分):

public class GCMIntentService extends GCMBaseIntentService { 
public GCMIntentService(){ 
     super(SENDER_ID);  
    } 
@Override 
    protected void onMessage(Context context, Intent intent) { 
     Log.i("Registration", "Got a message!"); 
      Log.i("Registration", context.toString() + " " + intent.toString()); 
    } 
... 
} 

客戶的清單:

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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 
    <permission android:protectionLevel="signature" android:name="com.example.myapp.permission.C2D_MESSAGE"></permission> 
    <uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE"/> 
    <!-- App 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" /> 
    <!-- Keeps the processor from sleeping when a message is received. --> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.myapp.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </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="com.example.myapp" /> 
      </intent-filter> 
     </receiver> 
     <service android:name=".GCMIntentService" /> 
    </application> 

</manifest> 

回答

0

嗯,你的消息得到肯定,因爲GCM發送到您的設備發送1.嘗試的成功狀態,以檢查你的應用程序的代碼GCMReceiver。

+0

感謝,有ISN是一個GCMReceiver代碼,只要我能理解,我的類擴展GCMBaseIntentService應該做接收? – dusm 2013-04-20 15:04:20

+0

是的。這就是我的意思。檢查接收器的正確性。因爲它似乎gcm發件人正在發送。所以檢查笏後出錯 – shiladitya 2013-04-20 16:03:26

2

爲了最大限度地提高接收郵件的機會,我不會在3秒鐘內設置time_to_live。我會完全忽略此參數,以便它採用默認值4周。我還會將delay_while設置爲false,以便手機即使在空閒時也會收到消息。

+0

謝謝,但它沒有幫助。服務器發送了它的消息,但即使模擬器在客戶端應用程序處於打開狀態,由於它不在logcat上,它看起來並沒有收到消息。 – dusm 2013-04-20 15:45:29

+0

可惜,除了用調試器檢查註冊ID是否正確之外(不知道該怎麼建議)(在'已註冊'位有一個斷點) – NickT 2013-04-20 15:48:47

0

改變你onMessage方法如下代碼:

Log.i("Registration", "Got a message!"); 
      Log.i("Registration", context.toString() + " " + intent.toString()); 

這樣:

String message=intent.getStringExtra("message"); 
Log.w("The message received is", message); 

希望這有助於