我需要帮助来实现GCM for android

时间:2012-10-17 09:34:29

标签: android google-cloud-messaging

我正在尝试实施GCM。

我实施了谷歌规定的全部内容。当我注册设备时,接收器中的回调没有任何影响。 GCMIntentService类中甚至没有onError()。

从不构造GCMIntentService。

这是我的代码......

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.roconmachine.gcm"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8"/>

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <permission android:name="com.roconmachine.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.roconmachine.gcm.permission.C2D_MESSAGE" /> 

    <!-- App receives GCM messages. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" /> 
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".NewGCMActivity"
                  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="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
          <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

          </intent-filter>
        </receiver>

        <service android:name=".services.GCMIntentService" android:enabled="true"/>
    </application>
</manifest>

这是

/*
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.roconmachine.gcm.services;



import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import com.google.android.gcm.GCMBaseIntentService;

/**
 * {@link IntentService} responsible for handling GCM messages.
 */
public class GCMIntentService extends GCMBaseIntentService {

    public GCMIntentService() {
        super("873601972999");
    }

    @Override
    protected void onRegistered(Context context, String registrationId) {
        Toast.makeText(this, "onRegistered", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Toast.makeText(this, "onUnregistered", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Toast.makeText(this, "onMessage", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDeletedMessages(Context context, int total) {
        Toast.makeText(this, "onDeletedMessages", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onError(Context context, String errorId) {
        Toast.makeText(this, "onError", Toast.LENGTH_LONG).show();
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        Toast.makeText(this, "onRecoverableError", Toast.LENGTH_LONG).show();
        return super.onRecoverableError(context, errorId);
    }


}


package com.roconmachine.gcm;

import com.google.android.gcm.GCMRegistrar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class NewGCMActivity extends Activity {
    private Button btnGetRegister;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnGetRegister = (Button)findViewById(R.id.button1);
        btnGetRegister.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                register();
            }
        });
    }

    private void register()
    {
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);

        if (GCMRegistrar.getRegistrationId(this).equals("")) {
            GCMRegistrar.register(this, "873601972999");
        } else 
        {
            Toast.makeText(this, "already register", Toast.LENGTH_LONG).show();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

确定。通过鸟瞰图没有特定的错误。你能尝试一件傻事吗?您已将GCMIntentService放入服务包中。你可以把它保存在主包中并测试一次。

答案 1 :(得分:0)

您需要将类别添加到接收器

<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="com.roconmachine.gcm" />
      </intent-filter>
    </receiver>

此外,您必须将服务移至应用程序包,文档说:

此意向服务将由GCMBroadcastReceiver(由GCM库提供)调用,如下一步所示。它必须是com.google.android.gcm.GCMBaseIntentService的子类,必须包含公共构造函数,并且应该命名为my_app_package.GCMIntentService(除非您使用GCMBroadcastReceiver的子类来覆盖用于命名服务的方法)