GCM客户端实现

时间:2014-02-04 10:49:32

标签: java android eclipse google-cloud-messaging

我正在按照此文档来实施我的Google Cloud Messaging客户端应用程序:http://developer.android.com/google/gcm/client.html除了“发送消息”步骤之外,我已经完成了所有步骤,因为我不需要“向服务器发送上游消息”功能

但是,我可以从GCM服务器获取“注册ID”并成功将其传递给我的服务器,但我无法在客户端应用程序中从我的服务器接收推送通知。 服务器端代码由另一个开发人员检查,一切似乎都没问题。问题出在我的客户端应用程序中。

也许我的广播接收器没有被触发,因为我没有在Google文档中实现“发送消息”部分?那部分:

public void onClick(final View view) {
if (view == findViewById(R.id.send)) {
    new AsyncTask() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {
                Bundle data = new Bundle();
                    data.putString("my_message", "Hello World");
                    data.putString("my_action",
                            "com.google.android.gcm.demo.app.ECHO_NOW");
                    String id = Integer.toString(msgId.incrementAndGet());
                    gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data);
                    msg = "Sent message";
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
            }
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
            mDisplay.append(msg + "\n");
        }
    }.execute(null, null, null);
} else if (view == findViewById(R.id.clear)) {
    mDisplay.setText("");
}
}

来自Google文档的报价:“广播接收器是GCM用于传递消息的机制。当onClick()调用gcm.send()时,它会触发广播接收器的onReceive()方法,该方法负责确保处理GCM消息。“因为我没有实现onClick()方法,所以我也没有实现gcm.send()方法。

如何触发广播接收器,它能够在我的情况下接收传入的推送通知?

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="des.gspa" android:versionCode="1"
android:versionName="1.0"  
 xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="9" 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" /> 

<permission android:name="des.gspa.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="des.gspa.permission.C2D_MESSAGE" />

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

<application android:icon="@drawable/ic_launcher"
     android:theme="@style/app_theme">
    <!-- GCM  -->
    <meta-data android:name="com.google.android.gms.version"
       android:value="@integer/google_play_services_version" />
    <receiver
        android:name=".GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="des.gspa" />
        </intent-filter>
    </receiver>
    <service android:name=".GcmIntentService" />

    <activity android:name="des.gspa.TabSample" 
     android:label="@string/app_name">
    </activity>     
    <activity android:name="des.gspa.MenuActivity"/>    
    <activity android:name="des.gspa.FeedbackActivity"/>
    <activity android:name="des.gspa.HomeActivity"/>
    <activity android:name="des.gspa.BookingActivity"/>
    <activity android:name="des.gspa.ContactActivity"/>
    <activity
        android:name="des.gspa.SplashScreen"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

    </manifest> 

GcmBroadcastReceiver.java

package des.gspa;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(),
            GcmIntentService.class.getName());
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}
}

0 个答案:

没有答案