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

Arm*_*ndo 2 android push-notification google-cloud-messaging

所以我在过去15个小时左右一直遇到问题,我跟着GCM开始使用Android(包括客户端演示源),据我所知,我应该在注册设备后很快收到推送通知并在发送ECHO_NOW消息后.这是我所做的和一些代码片段:

我一直在HTC One中运行代码(4.3)

  • 彻底遵循官方文档并创建使用Google Play服务lib(froyo包)的应用程序
  • 我创建并声明了一个主要的Activity,一个IntentService和一个WakefulBroadcastReceiver扩展的接收器.
  • 我确保检查GCM控制台,我有正确的发件人ID,启用到Android设备的推送通知,以及来自调试密钥库的指纹以及应用程序包.
  • 我添加了正确的权限,并确保我在权限和所需的Intent操作中拥有正确的包.

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>
Run Code Online (Sandbox Code Playgroud)

我假设在阅读此内容后会收到推送通知(包含在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.
Run Code Online (Sandbox Code Playgroud)

自从第一次尝试以来,设备成功地注册并获得注册ID.

我做错了假设并且没有收到推送通知吗?如果没有,那可能是什么错?

我已经尽可能地记录了,我应该发布更多代码吗?我已经检查了很多问题,没有人解决这个问题,或者只是不解决它.

在此先感谢社区.

此致

阿曼多

编辑:结论:GCM演示客户端工作正常,只需要记住,需要服务器端来请求GCM发送实际的推送通知.ECHO_NOW并不意味着GCM会代表您发送某些内容.实现服务器端!

标记答案(伊兰的)是第一个,但拉贾特为任何需要抬头的人提供了片段.谢谢你们俩.

raj*_*hai 6

实施服务器端代码以向您的设备发送通知.有关更多学习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;
?>
Run Code Online (Sandbox Code Playgroud)