Android C2DM and lib project

2019-07-23 06:26发布

问题:

I'm currently working on a libproject (Android) that should be included inside a few other applications.

Everything is working fine now that I've been struggling a bit with Activities and Manifests, exept for the C2DM bit.

I can invoke my different classes fine, but I can't seem to catch the registration ID (or of course actual messages, but that must be the same problem...)

I think the problem is coming from the filtering in my manifest(s), so if anyone has any advice for me, that would be really helpful.

Here is a copy of receiver part of my manifest (from the apps, not the library, but it's actually just a copy), but it's pretty straighforward. I just want to know how I should adapt it in order to invoke the right class in the lib...

        <!--
             Only C2DM servers can send messages for the app. If permission is 
            not set - any other app can generate it
        -->
        <receiver
            android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >

            <!-- Receive the actual message -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.myapp.lib" />
            </intent-filter>
            <!-- Receive the registration id -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.myapp.lib" />
            </intent-filter>
        </receiver>

Where com.myapp.lib is my lib package name, and the receiver is in a package named the same (in the lib project, of course).

Thanks in advance for the help, and don't hesitate to ask for furthers details :)

Edit : I tried with only the lib registered on google C2DM, and also with both app and lib. Same problem

回答1:

There is a better way of using your C2DM from a library project by using the intent-filter.

The manifest file is the one from the app. The package for the lib is com.mylib and the one for the app is com.myapp.

There is 2 things to change from the lib manifest.

  1. The classpath used in permissions.
  2. The intent-filter package.

Both should be you app package and not your lib package.

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

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

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

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >

     <uses-library android:name="com.google.android.maps" android:required="true"/>



    <activity
        android:name=".MyActivity"
        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.mylib.C2DMRegistrationReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" >
            </action>
            <category android:name="com.myapp" /> 
        </intent-filter>
    </receiver>

    <receiver
        android:name="com.mylib.C2DMMessageReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" >
            </action>
           <category android:name="com.myapp" /> 
        </intent-filter>
    </receiver>
</application>
</manifest>


回答2:

Answer, is anyone stumble upon the same problem...

In the google.android.c2dm package, class C2DMBaseReceiver, method runIntentInService, change

String receiver = context.getPackageName() + ".C2DMReceiver"

with the fully qualified name.. and there you go :)



回答3:

The manifest looks fine. In your package you should have a class called C2DMReceiver and it should extend C2DMBaseReceiver. This class and the overridden methods it contains are then called upon successful registration and when a message is received. I have written a very basic example of this that may be helpful for you to refer to here