简体   繁体   中英

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. Here's what I've done and some code snippets:

I have been running the code in an HTC One (4.3)

  • Followed the official documentation thoroughly and created the app making use of the Google Play Services lib (froyo package)
  • I created and declared a main Activity , an IntentService , and a WakefulBroadcastReceiver -extended receiver.
  • 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.
  • I added right permissions and also made sure I had the right package across the permissions and required Intent actions.

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

// 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.

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. ECHO_NOW does not mean that something will be sent by GCM in your behalf. Implement the server side!

Marked answer (Eran's) was the first one, BUT Rajat's provides snippet for whoever needs a heads up. Thank you both.

Implement a server side code to send notification to your device. For more learn Android GCM tutorial .

<?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. For that to work, you should implement the server side (which should establish a connection to the GCM Cloud Connection Server). This would also require you to fill a form in order to get your project ID white listed for the new GCM features.

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.

You can see samples of the required server side code here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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