Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On push notification show an activity/popup, instead of the message in the status bar

I am an android newbie and i really need your help .On push notification (GCMintentservice) i get to do stuff using pending intent. but I want to show a popup or an activity instead of a push notification message in status bar. That is if the app is running the user will have an activity displayed and NOT a message on status bar. I know its not recommended. But is that possible? Any help is appreciated.

Thanks in advance

like image 462
Vaseem Sathar Avatar asked Nov 11 '22 01:11

Vaseem Sathar


1 Answers

Yes! it is possible. On receiving push message from GCM, use following method:

public boolean isForeground(String myPackage) {
        ActivityManager manager = (ActivityManager) ctx
                .getSystemService(Activity.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager
                .getRunningTasks(1);

        ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(myPackage))
            return true;
        return false;
    }

You will come to know if your app is foreground or background. If it is background, show a notification else show an AlertDialog.

Above method needs this permission:

  <uses-permission android:name="android.permission.GET_TASKS" />
like image 115
Seshu Vinay Avatar answered Nov 14 '22 23:11

Seshu Vinay