0

我正在尝试实施 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();
        }
    }
}
4

2 回答 2

0

好的。通过鸟瞰,没有具体的错误。你能尝试一件愚蠢的事情吗?您已将 GCMIntentService 放入服务包中。你能不能把它放在主包里并测试一次。

于 2012-10-17T09:42:18.857 回答
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 的子类)。

于 2012-10-17T09:55:15.047 回答