4

I am trying to register to GCM from a library project the GCMIntentService is defined inside the library project.

Here is my registration code(inside the library project):

public static void init(Context context, String id){
    GCMRegistrar.checkDevice(context);
    GCMRegistrar.checkManifest(context);
    GCMRegistrar.register(context, id);
}

But the callbacks of the GCMIntentService are not being called, they are being called only when I run the library project as standalone android project.

Here is my test project manifest:

<?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>
Eran
  • 387,369
  • 54
  • 702
  • 768
user1940676
  • 4,348
  • 9
  • 44
  • 73
  • fallow tutorial http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ – abhi Jun 20 '13 at 11:23
  • @abhishesh that code works, what I am trying to achieve is starting it from a library project. – user1940676 Jun 20 '13 at 11:25
  • dont use library project . just put gcm.jar in your libs folder and project-property-java-build-path add jar then it will call – Deepanker Chaudhary Jun 20 '13 at 11:32
  • @DeepankerChaudhary But I need to use it as a library project. – user1940676 Jun 20 '13 at 11:36
  • have u make it library project if not then follow these steps i think problem will be solve: right click on library project-properties-android-then checked is Library checkbox then apply then in your project properties-android-add then apply may be called – Deepanker Chaudhary Jun 20 '13 at 11:40
  • @DeepankerChaudhary I know what a library project is. – user1940676 Jun 20 '13 at 11:41

1 Answers1

14

Here's what you have to do :

Write a class (in your library project) that overrides 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";
    }
}

Change your manifest to refer to the new receiver :

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

The reason that your original code didn't work is that the default implementation of getGCMIntentServiceClassName assumes that the intent service class is located in the package of the application, so it's expecting com.example.pushtest.GCMIntentService instead of com.test.pushlibrary.GCMIntentService.

Dan J
  • 25,433
  • 17
  • 100
  • 173
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Thank you!, and inside the project I just extend my custom GCMIntentService class which was implemented in the library project, am I right? – user1940676 Jun 20 '13 at 14:45
  • @user1940676 You're welcome. I specified all the changes you have to make. You don't have to extend your `GCMIntentService` which was implemented in the library project (unless you wish to add some logic specific to the Android project that uses the library project. And if you do extend it, you'll have to extend `PushLibraryBroadcastReceiver` too, in order to refer to that new subclass of GCMIntentService). – Eran Jun 20 '13 at 14:53
  • @Eran Hello i have a similar issue and i have posted a question as well but got no response so far. Could you please have a look at this : http://stackoverflow.com/questions/18505073/gcm-issues-below-android-4-i-a-library-project – Sayed Jalil Hassan Aug 29 '13 at 11:32
  • @Eran I have create structure as you mentioned here, but still not getting push notification in app. What has been missing by me in between, can you please guide me. I got stuck here I need help from you, please help! – Harish Godara Feb 14 '14 at 05:25
  • thank you ALOT! tried the whole day to get this to work, your solution made it! thumbs up! – dy_ Feb 19 '14 at 21:34
  • @datayeah please tell me how to structure this process to get push-notification from lib project. I'm stuck here. – Harish Godara Feb 21 '14 at 06:01
  • @Eran: If we are uploading our app(in which we have integrated library project) on play store then which app_id should I associate for GCM? – Harish Godara Sep 29 '14 at 05:05