【发布时间】:2012-10-19 17:07:03
【问题描述】:
我希望我不会做到这一点,但在过去的两天里,我一直在用一些看起来很简单的东西来搞砸我的脑袋。我已经按照google和vogella的教程实现了我的C2DM应用客户端+服务器。
我将尝试简要描述我的问题:
- 我的客户端应用程序成功收到了它的registrationId。
- 我的服务器成功收到了它的身份验证令牌。
- 服务器使用其身份验证令牌和设备的registrationId发送消息。
- 从服务器向客户端发送消息时,我得到一个成功的消息响应代码(例如 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。我错过了什么?
【问题讨论】:
-
你在其他设备上试过了吗?
-
这实际上是我的计划,但我没有其他支持此功能的 Android 设备。如果我不能管理,我会借一个朋友的设备试一试。
-
我认为这是重要的一步,因为它有助于缩小问题范围并排除一台设备的问题。也可以在不同的网络上尝试(3G 而不是 wifi) - 我看到 C2DM 消息无法到达某些网络(尽管您可以注册,这可能不是问题)。
-
看看该设备是否可以接收 C2DM 消息到其他应用程序也很有趣 - 例如。 WhatsApp 什么的。
-
我在使用 WhatsApp,它运行良好。我的应用程序现在成功运行(或者至少现在看起来是这样)。有关更多详细信息,请参阅我的答案。我猜可能是本地网络引起了问题,如果你说你以前见过这种行为。无论如何感谢您的 cmets!
标签: java android android-c2dm