I have been trying for a few hours to accomplish a seemingly simple task without success in Android. I have an app where users can earn achievements. There are calculated server-side and sent via push to my Android app. When I receive the push notification, I would like to do one of two things: (1) if my app IS running, I want to show a popup with the achievement info ( I have the layout for this in an xml). (2) if my app is not running, post a notification to the notification bar.
First question: I cannot do (1) no matter how hard I try. I have tried starting a new activity from the GCM IntentService OnHandleIntent like this:
int achID = jo.getInt("AchievementID");
Intent newAchievementIntent = new Intent(getBaseContext(), NewAchievementActivity.class);
newAchievementIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newAchievementIntent.putExtra("AchievementID", achID);
getApplication().startActivity(newAchievementIntent);
This simply does nothing. I then tried making all my activities inherit from a special IsRunningActivity, which has a broadcast listener that then starts a new activity, as such:
public class IsRunningBaseActivity extends Activity
{
public static boolean isRunning = false;
public static IsRunningBaseActivity currentActivity = null;
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
currentActivity = this;
isRunning = true;
LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
int achId = intent.getExtras().getInt("AchievementID");
Intent newAchievementIntent = new Intent(IsRunningBaseActivity.this, NewAchievementActivity.class);
newAchievementIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newAchievementIntent.putExtra("AchievementID", achId);
IsRunningBaseActivity.this.startActivity(newAchievementIntent);
}
}, new IntentFilter(CLServices.NOTIFICATION_NEW_ACHIEVEMENT));
}
@Override
protected void onPause()
{
super.onPause();
isRunning = false;
};
}
So now the IntentService broadcasts an intent which is then picked up by the running activity, which then tried to start the NewAchievementActivity. Still no luck. The activity does not start.
What am I doing wrong here? What is the best way to accomplish what I want here?
Second question: When I show a notification from push because the app is NOT running, I then want the user to be able to tap the notification to open my app and show that same NewAchievementActivity. The problem is, that activity depends on an application context (the user needs to be logged in and have a LoginToken so that I can retrieve his achievement data to show). So how do I open that activity while still going through my app's normal flow? (LoginActivity, HomeActivity, NewAchievementActivity)? Is this even possible?
Thank you very much for helping.