简体   繁体   English

GCM的Android客户端没有收到回显推送通知

[英]Android client for GCM not receiving echoing push notification

So I've been having an issue for the past 15 hours or so, I followed GCM's getting started for Android (client demo source included) and as far as I know, I'm supposed to receive a push notification soon after registering the device and after sending an ECHO_NOW message. 因此,在过去15个小时左右的时间里,我一直遇到问题,我关注GCM的Android使用入门(包括客户端演示源),据我所知,我应该在注册设备后立即收到推送通知并在发送ECHO_NOW消息后。 Here's what I've done and some code snippets: 这是我所做的和一些代码片段:

I have been running the code in an HTC One (4.3) 我一直在HTC One(4.3)中运行代码

  • Followed the official documentation thoroughly and created the app making use of the Google Play Services lib (froyo package) 完全遵循官方文档,并使用Google Play服务lib(froyo程序包)创建了应用
  • I created and declared a main Activity , an IntentService , and a WakefulBroadcastReceiver -extended receiver. 我创建并声明了一个主Activity ,一个IntentService和一个WakefulBroadcastReceiver接收器。
  • I made sure to check the GCM Console, I have the right Sender ID, Enabled push notif to Android devices, and fingerprint from the debug keystore, along with the app package. 我确保检查了GCM控制台,我拥有正确的发件人ID,向Android设备启用的推送通知,以及调试密钥库中的指纹以及应用程序包。
  • I added right permissions and also made sure I had the right package across the permissions and required Intent actions. 我添加了正确的权限,并确保我在所有权限和必需的Intent操作中都拥有正确的程序包。

AndroidManifest.xml: AndroidManifest.xml中:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.package"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission android:name="my.package.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="my.package.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="my.package.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="my.package.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="my.package" />
            </intent-filter>
        </receiver>

        <service android:name="my.package.GcmIntentService" />

    </application>
</manifest>

I made an assumption that I would receive a push notification after reading this (included in the source code for the gcm client demo implementation): 我假设阅读此内容后会收到推送通知(包含在gcm客户端演示实现的源代码中):

// For this demo: we don't need to send it because the device
// will send upstream messages to a server that echo back the
// message using the 'from' address in the message.

Ever since the first try, the devices is being successful in registering and obtaining the registration id. 自从第一次尝试以来,设备成功地注册并获得注册ID。

Question

Did I make the wrong assumption and no push notification should be received? 我是否做出了错误的假设,不应收到任何推送通知? If not, what could be wrong? 如果没有,那可能是错的?

I've documented as much as possible, should I post more code? 我已经尽可能地记录了,我应该发布更多代码吗? I've checked so many questions and none has tackled this exact problem or just won't fix it. 我检查了很多问题,没有一个解决这个确切的问题,或者只是解决不了。

Thanks in advance to the community. 在此先感谢社区。

Sincerely, 此致

Armando 阿曼多

EDIT: Conclusion: GCM Demo Client works perfectly, just need to keep in mind that a server end is needed to request GCM to send the actual push notification. 编辑:结论:GCM演示客户端工作正常,只需要记住,需要服务器端来请求GCM发送实际的推送通知。 ECHO_NOW does not mean that something will be sent by GCM in your behalf. ECHO_NOW并不意味着GCM会代您发送一些东西。 Implement the server side! 实现服务器端!

Marked answer (Eran's) was the first one, BUT Rajat's provides snippet for whoever needs a heads up. 带标记的答案(伊朗)是第一个,但是Rajat的片段为需要抬头的人提供摘要。 Thank you both. 谢谢你俩。

Implement a server side code to send notification to your device. 实施服务器端代码以将通知发送到您的设备。 For more learn Android GCM tutorial . 有关更多信息,请学习Android GCM教程

<?php
    $api_key = "AIzBpx4XbXwKd8P-v2d1Jk";
    $registrationIDs = array("APA91b3Rjlo8T_URx15NqvxY2mRyQ");
    $message = "congratulations";
    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array(
                'registration_ids'  => $registrationIDs,
                'data'              => array( "message" => $message ),
                );

    $headers = array(
                    'Authorization: key=' . $api_key,
                    'Content-Type: application/json');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
    $result = curl_exec($ch);
    curl_close($ch);

echo $result;
?>

The GCM Demo App you followed uses the new device to cloud messaging (upstream messages) to send a message to a server and it assumes the server echoes back that message. 您遵循的GCM演示应用程序使用新设备进行云消息传递(上游消息)以向服务器发送消息,并假定服务器回显该消息。 For that to work, you should implement the server side (which should establish a connection to the GCM Cloud Connection Server). 为此,您应该实现服务器端(应该建立与GCM Cloud Connection服务器的连接)。 This would also require you to fill a form in order to get your project ID white listed for the new GCM features. 这也将需要您填写一个表格,以便为新的GCM功能列出白色的项目ID。

If you don't need the new GCM features (device to cloud and user notifications), you should simply send the registration ID to your server and send a message from the server to your device. 如果您不需要新的GCM功能(设备到云和用户通知),您只需将注册ID发送到服务器并从服务器向您的设备发送消息。

You can see samples of the required server side code here . 您可以在此处查看所需服务器端代码的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM