设备无法通过GCM接收消息

3
我按照谷歌的指示启动了GCM服务器和客户端。但是,尽管我从服务器收到了成功的GCM消息:

MulticastResult(multicast_id=8287827393174436535,total=1,success=1,failure=0,canonical_ids=0,results: [[ 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);
        }

and gcmIntentServiceCreated (partly) :

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>
3个回答

2
为了增加接收消息的机会,我建议不要将 time_to_live 设置为仅仅 3 秒。最好省略这个参数,让它采用默认值 4 周。同时,将 delay_while 设置为 false,这样手机即使处于空闲状态也能接收到消息。

谢谢,但是它没有帮助。服务器发送了它的消息,但即使模拟器在客户端应用程序中打开,看起来消息也没有被接收,因为它不在logcat上。 - dusm
很遗憾,除了通过调试器检查注册ID是否正确(在“已注册”部分设置断点)之外,我不知道还有什么建议。 - NickT

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);

希望这可以帮到你


0

你的消息肯定已经被发送到你的设备了,因为GCM发送了一个成功状态1。尝试检查你的应用程序的GCMReceiver代码。


感谢您的提问。据我所知,没有GCMReceiver代码。我的类扩展了GCMBaseIntentService应该完成接收工作。 - dusm
没错,那就是我的意思。检查接收者的正确性,因为似乎 GCM 发送器正在发送。所以在此之后检查出了什么问题。 - shiladitya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接