设备未接收有效负载消息,但正在接收注册消息



目前正在使用c2dm构建一个android应用程序,从应用程序引擎上构建的网站推送消息。我已经学习了很多教程,到目前为止。。。

  • 我已经在c2dm上开立了一个账户,并获得了批准
  • 让我的设备在C2DM服务器上注册(接收注册Id)
  • 将此注册Id与设备的链接电子邮件一起推送到存储该Id的应用程序
  • 向Google的ClientLogin发送请求,并获取有效的Auth令牌
  • 使用有效负载、regId和auth令牌向C2DM服务器发出post请求。我也收到来自c2dm服务器的"Ok"状态代码(200)。所以我认为一切顺利

但是,我的设备只是放在那里,从来没有收到任何信息。在过去的一周里,我多次将该项目拆开并从头开始重建,但它总是回到设备上,只是没有收到有效负载消息。我不知道哪里出了问题。

我的帐户可能有问题吗?我是否可以检查C2DM服务器上的挂起消息

代码位:安卓清单

<permission android:name="skaught.wingman.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="skaught.wingman.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:name="C2DReceiver" android:permission="com.google.android.c2dm.permission.SEND">
        <!-- Receive registration ids -->
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="skaught.wingman" />
        </intent-filter>
        <!-- Receive actual messages -->
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="skaught.wingman" />
        </intent-filter>
    </receiver>
    ....

向C2DM服务器发送推送

payload = {
    "data.payload" : "Please Work!",
    "registration_id": regId,
    "collapse_key": hash(email),
}
encodedPayload = urllib.urlencode(payload)
url = "http://android.clients.google.com/c2dm/send"
#Make a POST request to C2DM server
result = urlfetch.fetch(url=url,
                        payload=encodedPayload,
                        method=urlfetch.POST,
                        headers={   'Content-Type': 'application/x-www-form-urlencoded',
                                    'Authorization': 'GoogleLogin auth=' + authToken}
                        )

在安卓上接收C2DM消息

public class C2DReceiver extends BroadcastReceiver {
  @Override
    public final void onReceive(Context context, Intent intent) {
       Log.d(Constants.TAG, "Received a Message");        
        if (Constants.RECEIVED_REGISTRATION_ID_FROM_GOOGLE.equals(intent.getAction())) {            
            Log.d(Constants.TAG, "Received a registration ID from Google.");
            handleRegistration(context, intent);
        } else if (Constants.RECEIVED_C2DM_MESSAGE_FROM_GOOGLE.equals(intent.getAction())) {            
            //I'm NEVER reached!
            Log.d(Constants.TAG, "Received a C2DM message from Google.");
        }
    }

请确保用于在电话上获取注册id的电子邮件id和用于获取身份验证令牌的电子邮件id是相同的。

还要确保您的有效负载不超过1024个字符。有效负载有1024个字符的限制。

您还必须确保您在批准时使用电子邮件id注册的包名称相同。我想你已经这么做了。

  1. 首先,您必须从C2DM接收注册id到您的设备

  2. 使用此设备在您的应用服务器中注册id并获取身份验证令牌

  3. 使用此身份验证和设备注册id发送有效载荷

但由于您没有按顺序进行操作,因此您的设备注册id将再次更改,但您使用的是旧id。因此您无法在设备中获取消息当你像这样执行时。

请遵循第4章中提供的示例应用程序:
http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html

最新更新