2013-04-20 150 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); 

希望这有助于