Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C2DM Broadcast Receiver

Tags:

android

I had a working C2DM application. I reused the same package name when creating a new C2DM application.

It works except now the BroadcastReceiver doesn't get called when the application is not running. That is to say it all works if I run the application and send it C2DM messages. But after a force quit the BroadcastReceiver is no longer called.

I have looked at a lot of examples and compared everything in my old manifest to the new one. Paying extra attention to package names used in categories, Intent Services, etc..

Question: Is there a common C2DM coding/config mistake that results in the BroadcastReceiver not being called after an app is force quit?

I do get this log cat when I send a C2DM message after force quit of my application:

01-11 00:54:43.580: WARN/GTalkService(286): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE cat=[com.aawwpcd.pcd3] (has extras) }

I get one of those for each C2DM message I send to the device after force quitting the application.

It looks like the intent is coming in but not being passed to my BroadcastReceiver.

Edit:

Here are the relavant bits from the Manifest and BroadcastReceiver:

BroadcastReciever

package com.aawwpcd.pcd3.c2dm;

import ...

public class C2DMBroadcastReceiver extends BroadcastReceiver {

    @Override
    public IBinder peekService(Context myContext, Intent service) {
        return super.peekService(myContext, service);
    }

    public C2DMBroadcastReceiver() {
        super();
    }

    @Override
    public void onReceive(Context context, Intent intent) {

    ...

    }

}

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.aawwpcd.pcd3"
      android:versionCode="250"
      android:versionName="ICSPCD3">

<uses-sdk android:minSdkVersion="13"
          android:targetSdkVersion="14"/>

<permission android:name="com.aawwpcd.pcd3.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.aawwpcd.pcd3.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.WAKE_LOCK"/>

<application android:name=".PCD3Application"
             android:label="@string/app_name"
             android:icon="@drawable/pcdlauncher"
             android:theme="@android:style/Theme.Holo">

    <activity android:name=".honeycombpcd3.FullScheduleActivity"
              android:label="@string/app_namefull"
            >

        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>

    </activity>

    <!-- Only C2DM servers can send messages for the app. If permission is not
    set - any other app can generate it -->
    <receiver android:name=".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.aawwpcd.pcd3"/>
        </intent-filter>

        <!-- Receive the registration id -->
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
            <category android:name="com.aawwpcd.pcd3"/>
        </intent-filter>

    </receiver>

</application>

</manifest>

Edit: Could this be something new in 3.x ? As stated above my problems started with this new application - written for 3.x. What I want is for the C2DM to call the BroadcastReceiver even when the application is not running. I am not seeing that. Could it be a 3.x change? It worked this way before on 2.3.x phones and I can't find anything I am doing different. It will be a hassle to write the test code to prove this but that's next I guess.

Edit: Seems related to Force Quit. I don't have any problem when I re-install the .apk and then send the device a c2dm message; the Broadcast receiver picks it up. In this case the application was not already running when the C2DM came in yet everything worked as expected. The only problem I am having is after I force quit an application. C2DM messages after that are not picked up by the BroadcastReceiver.

like image 913
Ayyrk Avatar asked Jan 11 '12 05:01

Ayyrk


2 Answers

Have a look at https://stackoverflow.com/a/7108611

And at Android 3.1 release notes http://developer.android.com/about/versions/android-3.1.html#launchcontrols

As of 3.1, Force stopped apps no longer get restarted by C2DM by default. There is a new flag for requesting a restart after force-close.

like image 50
Doron Zehavi Avatar answered Sep 20 '22 19:09

Doron Zehavi


Have you been monitoring logcat after you "force quit" your receiver a few times? It is likely that you will see a "Bad Process" message implying that your receiver has crashed too many times and Android will no longer send it intents. Restarting the phone / reinstalling the application fixes this problem as you implied in your edit.

like image 42
Justin Breitfeller Avatar answered Sep 23 '22 19:09

Justin Breitfeller