GCM onRegistered没有打电话

时间:2012-09-12 12:11:22

标签: android google-cloud-messaging

我已经为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());
        }
    }

}

2 个答案:

答案 0 :(得分:8)

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

的2个地方

答案 1 :(得分:1)

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