roiberg
roiberg

Reputation: 13747

Keep app in running state to receive push from urban airship

My app uses urban Airship for its push notification.

The problem starts when a user "force" closes the app (by sliding it away in the multitasking window or in the settings). When a push arrives, it gets to urban airship's receiver and they are broadcasting an intent for my app to use. So, if the app is forced closed then my receiver will not be activated and I can't receive the broadcast.

I know about Intent.FLAG_INCLUDE_STOPPED_PACKAGES but I cant use it because urban is doing the broadcast and its in a jar file.

A fix could be that I keep my app in a running state all time, even if the user closes it. How can I do that? I see that What's app is doing that. Is there another solution?

P.S. I know that android is build in a way that when a user force closes an app he "wants" it to deactivate it's receivers, but it's not the same for push notification, push notification will still get through. I only want my app to "live" so I can receive the push. Thanks!

Upvotes: 1

Views: 817

Answers (2)

linakis
linakis

Reputation: 1231

You'll want your service to return the START_STICKY flag

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    handleCommand(intent);
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

And to persist across reboots you have to create and register a receiver that extends BroadcastReceiver that starts your service in it's onReceive.

In your manifest add Permission:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Register Receiver:

<receiver
    android:name=".MyBootReceiver"
    android:enabled="true"
    android:exported="false"
    android:label="MyBootReceiver" >
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <!-- Catch HTC FastBoot -->
    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>

Upvotes: 0

Lena Bru
Lena Bru

Reputation: 13947

services live when the app is closed, try receiving your broadcast through a service that is stopped only when service.stopSelf() is called from within the service

Upvotes: 1

Related Questions