简体   繁体   中英

Show popup from push notification when app is running

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.

The following code should answer your first question. It has worked for me using GCM to update my listview when a it comes in. The question can be found Refreshing activity on receiving gcm push notification The link will explain further. Hope it helps.

@Override
public void onResume() {
super.onResume();
context .registerReceiver(mMessageReceiver, new IntentFilter("unique_name"));
}

//Must unregister onPause()


@Override

protected void onPause() {

super.onPause();

context.unregisterReceiver(mMessageReceiver);
}


//This is the handler that will manager to process the broadcast intent
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override

public void onReceive(Context context, Intent intent) {

    // Extract data included in the Intent
    String message = intent.getStringExtra("message");

    //do other stuff here
}

};

The second question if i understand it correctly. You can specify what happens on a notification click. So just send the user where you want them to go.

Intent intent= new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,intent, 0);

I figured out what the problem was. I just wasn't calling "setContentView" in the activity I was trying to open. I knew it had to be some stupid mistake like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM