简体   繁体   English

当我管理推送通知时,如何知道我的应用是否已打开

[英]How to know if my app is open when I managed a push-notification

使用Android,当我收到通知推送我的GCMIntentService时,我想知道我的应用程序是否打开,因为如果我的应用程序打开时用户点击通知我想什么都不做,但如果应用程序关闭我想打开这款应用。

Launch the root activity (the one that has ACTION=MAIN and CATEGORY=LAUNCHER in the manifest) and add Intent.FLAG_ACTIVITY_NEW_TASK . 启动根活动(清单中具有ACTION = MAIN和CATEGORY = LAUNCHER的活动)并添加Intent.FLAG_ACTIVITY_NEW_TASK If the app is already active (no matter which activity is on top) this will just bring the task to the front. 如果应用程序已经处于活动状态(无论哪个活动位于顶部),这只会将任务置于最前面。 If the app isn't active, it will start it with your root activity. 如果应用程序未处于活动状态,则会以您的根活动启动它。

Define this in all the activities : 1.) Define a static final boolean flag named 'check_running mode' 2.) Define(override) onResume() and onPause() methods in all activities. 在所有活动中定义:1。定义名为'check_running mode'的静态最终布尔标志.2。)在所有活动中定义(覆盖)onResume()和onPause()方法。 3.) Set the value of this falg as 'true' in onResume() and 'false' in OnPause() methods respectively. 3.)分别在OnResume()和OnPause()方法中将此falg的值设置为'true',将'false'设置为'false'。 4.) Check out when u receive the push notification : a. 4.)检查何时收到推送通知:a。 If falg value is true it means app is in forground so do nothing in that case b. 如果falg值为true,则意味着app处于forground状态,因此在这种情况下不执行任何操作b。 If flag value is false it means app is in background so You can open the app in that case 如果标志值为false,则意味着应用程序处于后台,因此您可以在这种情况下打开应用程序

NOTE: The falg must be static final as you can change it from any of the activity and access it in your receiver class simply. 注意:falg必须是静态final,因为您可以从任何活动中更改它并简单地在接收器类中访问它。 I hope it'll work for you! 我希望它对你有用!

1 :
static boolean check_running mode = false;


-------------------
2:
@Override
protected void onResume() {
    super.onResume();

    check_running mode = true;
}

@Override
protected void onPause() {
    check_running mode = false;

    super.onPause();
}

---------------------
3 :
if (check_running mode) {
    showUserView();
}
else {
    showNotification();
}
public static boolean isAppRunning(Context context) {
    // check with the first task(task in the foreground)
    // in the returned list of tasks
    ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> services = activityManager
            .getRunningTasks(Integer.MAX_VALUE);
    if (services.get(0).topActivity.getPackageName().toString()
            .equalsIgnoreCase(context.getPackageName().toString())) {
        // your application is running in the background
        return true;
    }
    return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-收到推送通知后如何检查我的应用程序是否打开? - Android - How to check my app is open when receive a push-notification? 如何在应用前台制作信号推送通知警报 - How to make onesignal push-notification alert in app foreground 使用推送通知时如何获取用户设备令牌 - how to get the users device token when using push-notification 当用户注销并收到推送通知时,我的应用程序会自动打开。 我想通过点击推送通知来打开我的应用程序? - When user logged out and the push notification come my app opens automatically. i want to open my app on the click of push notification? 短信推送通知 - SMS push-Notification 加载应用程序之前的默认推送通知白色方块 - Default push-notification white square before app is loaded 如何在我的应用程序的WebView组件中打开推送通知? - How to open the push notification in the WebView component of my app? 离子1信号有一种方法可以知道从背景到前景打开应用程序或启动应用程序时是否有推送通知 - ionic 1 onesignal is there a way to know if there is a push notification when open app from background to foreground or launching app 如何在收到推送通知时自动打开应用程序? - How to automatically open app when receive push notification? 当应用程序无法在后台运行时,如何在点击推送通知中打开网址 - how to open url on click push notification when app is not working in background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM