简体   繁体   中英

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

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" />

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