0

我已经为 GCM 注册编写了代码,但我不知道为什么我没有收到 GCMIntentService 的任何回调(onRegistered,onError)。请让我知道或指导我解决这个问题。
这是我的清单:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pushnotifydemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <permission
        android:name=".permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".PushNotifyDemo"
            android:label="@string/title_activity_push_notify_demo" >
            <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" />
                <category android:name="com.example.pushnotifydemo" />
            </intent-filter>
        </receiver>

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

    </application>

</manifest>

这是我的 CMIntentService:

 package com.example.pushnotifydemo;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

    public GCMIntentService() {
        super("****");
        Toast.makeText(this, "Constructor Called....  :  ", Toast.LENGTH_LONG)
                .show();
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onError(Context arg0, String arg1) {
        // TODO Auto-generated method stub
        Log.i("ibad", "Error" + arg1);
    }

    @Override
    protected void onMessage(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onRegistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Registration key :  " + arg1, Toast.LENGTH_LONG)
                .show();
        // System.out.println("GCMIntentService.onRegistered()");
        // Log.i("ibad","authr" + arg1);
    }

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub
        Log.i("ibad", "onUnregistered" + arg1);
    }

}

这是我的活动:

   package com.example.pushnotifydemo;

import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gcm.GCMRegistrar;


public class PushNotifyDemo extends Activity {

    private static final String TAG = "PUSHDEMO";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_push_notify_demo);

        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
            Toast.makeText(this, "Not Registered key \n\n\n:  " + regId , Toast.LENGTH_LONG).show();
          GCMRegistrar.register(getApplicationContext(), "****");
        } else {
//          Log.v("ibad", "Already registered");
            //Toast.makeText(this, "Already Registered key \n\n\n:  " + regId , Toast.LENGTH_LONG).show();
            GCMRegistrar.unregister(getApplicationContext());
        }
    }

}
4

2 回答 2

8

在它们出现的两个地方替换<uses-permission android:name=".permission.C2D_MESSAGE" /><uses-permission android:name="com.example.pushnotifydemo.permission.C2D_MESSAGE" />Manifest

于 2012-09-12T12:15:13.183 回答
1
 <permission
    android:name="your_package_name.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission
    android:name="your_package_name.permission.C2D_MESSAGE" />
于 2012-09-12T12:15:10.677 回答