3

I've got an Android app with push notifications set up using Urban Airship. The notifications work fine while my app is open but I need to still receive notifications when my app is closed. I've looked around but haven't found something that works. I'm pretty sure the problem is in my manifest, so here it is.

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

    <uses-sdk android:minSdkVersion="11" />

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

    <!-- Permissions for Urban Airship -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <!-- Not sure if required for C2DM -->
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />

    <!-- Only this application can receive the messages and registration result -->
    <permission android:name="za.co.foo.android.financials.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="za.co.foo.android.financials.permission.C2D_MESSAGE" />

    <!-- This app has permission to register and receive message -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name=".InvestorRelations"
            android:label="@string/app_name" 
            android:screenOrientation="landscape" 
            android:theme="@android:style/Theme.Holo.NoActionBar">

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

        <!-- For Urban Airship -->
        <receiver android:name="com.urbanairship.CoreReceiver">
        </receiver>

        <receiver android:name="com.urbanairship.push.c2dm.C2DMPushReceiver"
                android:permission="com.google.android.c2dm.permission.SEND"
                android:enabled="true">
             <!-- Receive the actual message -->
             <intent-filter>
                 <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                 <category android:name="za.co.foo.android.financials" />
             </intent-filter>
             <!-- Receive the registration id -->
             <intent-filter>
                 <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                 <category android:name="za.co.foo.android.financials" />
             </intent-filter>
        </receiver>

        <receiver android:name="za.co.foo.android.financials.IntentReceiver" />

        <service android:name="com.urbanairship.push.PushService" />

    </application>

</manifest>

Here is my IntentReceiver

public class IntentReceiver extends BroadcastReceiver {

    private static final String logTag = "IR Intent Receiver";

    @Override
        public void onReceive(Context context, Intent intent) {
        Log.i(logTag, "Received intent: " + intent.toString());
        String action = intent.getAction();

        if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
            int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0);

            Log.i(logTag, "Received push notification. Alert: "
                    + intent.getStringExtra(PushManager.EXTRA_ALERT)
                    + " [NotificationID="+id+"]");

            logPushExtras(intent);

        } else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
            Log.i(logTag, "User clicked notification. Message: " + intent.getStringExtra(PushManager.EXTRA_ALERT));

            logPushExtras(intent);

            Intent launch = new Intent(Intent.ACTION_MAIN);
            launch.setClass(UAirship.shared().getApplicationContext(), InvestorRelations.class);
            launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            launch.putExtra("FromNotification", true);

            UAirship.shared().getApplicationContext().startActivity(launch);

        } else if (action.equals(PushManager.ACTION_REGISTRATION_FINISHED)) {
            Log.i(logTag, "Registration complete. APID:" + intent.getStringExtra(PushManager.EXTRA_APID)
                    + ". Valid: " + intent.getBooleanExtra(PushManager.EXTRA_REGISTRATION_VALID, false));
        }

    }

    public void logPushExtras(intent) {
        //Does some logging stuff
    }
}

Any help would be great

Edit

Included the entire manifest with only my company name changed to "foo".

Edit

As it turns out, my app does receive notifications while closed, but not after a force-close. Realised after reading this question.

Community
  • 1
  • 1
Mike T
  • 4,747
  • 4
  • 32
  • 52
  • When you receive a push message when your app is not active, how you want to show your notification? Do you want to use android notification(status bar)? – Shrikant Ballal May 30 '12 at 12:39
  • Yes. At the moment the push message shows in the notification bar. I just want it to do the exact same when my app is not open – Mike T May 30 '12 at 12:46
  • You get them when it is active? (ei. push messages gets delivered) ? – Anders Metnik May 30 '12 at 14:54
  • Yes, push messages get delivered and displayed if my app is active or open in the background – Mike T May 31 '12 at 05:39
  • Need to see your C2DMPushReceiver then, since it is the one that is supposed to handle stuff in background. If it is you pass the received on to a service or an activity then you need to aquire wakelock to make you have "CPU time" – Anders Metnik May 31 '12 at 05:57
  • I've added in the BroadcastReceiver now. Something about a wakelock sounds promising as an answer. Can you write a bit more as an answer so I can try? – Mike T May 31 '12 at 06:14
  • Oh, I've misunderstood you. C2DMPushReceiver is an Urban Airship thing, hence the package is not my package. I'm also wondering now how this is meant to link to my actual code. My code does run though – Mike T May 31 '12 at 08:04
  • Hehe okay, quite important info that it was force closed ;-) Ohh well, answer your own question and accept it as answer imho. Maybe give me a +1 for nice help ;) – Anders Metnik Jun 01 '12 at 05:11

2 Answers2

2

It turns out the notifications worked fine whether the app was running or not. The problem was when the app was force-closed.

I think the actual problem is something along the lines of the BroadcastReceiver being killed when the app is force closed but I'm not sure.

This is just to close the question off because it's unlikely to be solved now that I realise the problem is different to the question title.

Mike T
  • 4,747
  • 4
  • 32
  • 52
1

is your projects package really named "mypackage" ? and is your receiver etc. in that package? Because it looks like all "myPackage" should be repalced with "com.urbanairship"

Anders Metnik
  • 6,096
  • 7
  • 40
  • 79
  • No, it's not really named "myPackage". That is to remove sensitive info from the question. From this tutorial: https://docs.urbanairship.com/display/DOCS/Getting+Started%3A+Android%3A+C2DM+Push I've replaced all "com.urbanairship.push.sample" with my package name as this seems to be what is required – Mike T May 30 '12 at 12:53
  • Well if you think your packagename is really that sensitive a subject, then im afraid i can't help you. since i can't find heads or tails in your xml file since some things are called "com.urbanairship" and some "mypackage" how can i know which one u have typed wrong and which on correctly? and how to know that they really match your package. I have dealt a lot with this in c2dm, and it is a very big issue. – Anders Metnik May 30 '12 at 12:58
  • Edited the question to include the entire manifest and hide less package info – Mike T May 30 '12 at 13:18
  • Good, im a bit disturbed that you have receiver in 1 package, and the intentfilters pointing at a completely other one. But if u receive the push msg' it should be fine, and then i need to see your receiver code. – Anders Metnik May 30 '12 at 14:56
  • This is the first time I've had to use receivers so there may be plenty wrong with it. I'll take whatever suggestions are given. I just copied and modified the Urban Airship sample code. I'll edit my question now – Mike T May 31 '12 at 05:42