Android 从库项目中注册GCM

Android 从库项目中注册GCM,android,push-notification,android-service,google-cloud-messaging,Android,Push Notification,Android Service,Google Cloud Messaging,我正在尝试从库项目注册到GCM。GCMinentService是在库项目中定义的 这是我的注册码(在库项目中): 但是gcminentservice的回调没有被调用,只有当我作为独立的android项目运行library项目时才会被调用 这是我的测试项目清单: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"

我正在尝试从库项目注册到GCM。GCMinentService是在库项目中定义的

这是我的注册码(在库项目中):

但是gcminentservice的回调没有被调用,只有当我作为独立的android项目运行library项目时才会被调用

这是我的测试项目清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pushtest"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.example.pushtest.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".PushTestApp" >
        <activity
            android:name="com.example.pushtest.MainActivity"
            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" />

                <category android:name="com.example.pushtest" />
            </intent-filter>
        </receiver>
    </application>
    //push library definition
    <service android:name="com.test.pushlibrary.GCMIntentService" />

</manifest>

//推送库定义

以下是您必须执行的操作:

编写一个覆盖GCMBroadcastReceiver的类(在库项目中):

package com.test.pushlibrary;

import android.content.Context;

import com.google.android.gcm.GCMBroadcastReceiver;


public class PushLibraryBroadcastReceiver extends GCMBroadcastReceiver
{
    /**
     * Gets the class name of the intent service that will handle GCM messages.
     */
    @Override
    protected String getGCMIntentServiceClassName(Context context) {
        return "com.test.pushlibrary.GCMIntentService";
    }
}
更改您的清单以引用新的接收者:

    <receiver
        android:name="com.test.pushlibrary.PushLibraryBroadcastReceiver"
        ...

fallow tutorial@abhishesh既然代码是有效的,我想实现的是从一个库项目开始。不要使用库项目。只需将gcm.jar放在您的libs文件夹和项目属性java build path add jar中,就可以了call@DeepankerChaudhary但我需要把它作为一个库项目来使用。如果没有,让你创建一个库项目,然后按照以下步骤操作,我认为问题会得到解决:右键单击库项目属性android,然后选中是库复选框,然后应用然后在您的项目属性中,android add-then-apply可能会被称为谢谢!,在这个项目中,我只是扩展了我在library项目中实现的自定义GCMinentService类,对吗?@user1940676不客气。我指定了您必须进行的所有更改。您不必扩展在库项目中实现的
gcminentservice
(除非您希望添加一些特定于使用库项目的Android项目的逻辑。如果您确实扩展了它,您还必须扩展
PushLibraryBroadcastReceiver
,以引用GCMinentService的新子类)@Eran你好,我有一个类似的问题,我也发布了一个问题,但到目前为止还没有得到回应。你能看一下这个:@Eran我已经创建了你在这里提到的结构,但仍然没有在应用程序中得到推送通知。其间我遗漏了什么,请你指导我。我被困在这里,我需要你的帮助,恳求谢谢你的帮助!非常感谢!花了整整一天的时间才让它发挥作用,你的解决方案成功了!竖起大拇指!
    <receiver
        android:name="com.test.pushlibrary.PushLibraryBroadcastReceiver"
        ...