简体   繁体   English

在 iOS 上收到通知时确定应用程序是否在前台运行

[英]Determine if an app is running in the foreground when a notification is received on iOS

I would like to find a way to see what app is running in foreground or if the home screen is displayed when a local notification from my app show up.我想找到一种方法来查看哪些应用程序在前台运行,或者当我的应用程序显示本地通知时是否显示主屏幕。 For example i want to have different actions if there is in homescreen or in someone else app.例如,如果在主屏幕或其他应用程序中,我想有不同的操作。 I tried to use processed and pid but the pid is generated hen the app starts and not the last time the app is used.我尝试使用处理过的和 pid 但 pid 是在应用程序启动时生成的,而不是最后一次使用应用程序时生成的。 Any idea?任何的想法? thanks谢谢

推送通知文档中所述,您可以在收到通知时阅读[[UIApplication sharedApplication] applicationState]以确定您的应用程序是在前台、非活动状态(它是可见的,但像 WiFi 选择器这样的对话框在前面)还是在后台。

Just to have a copy-paste code available for others:只是为了让其他人可以使用复制粘贴代码:

if([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
    //App is in foreground. Act on it.
}

Swift 5 version:斯威夫特 5 版本:

import UIKit
let isForeground = UIApplication.shared.applicationState == .active

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

相关问题 当收到静默推送通知并且应用程序在后台,iOS中运行时,执行几行代码 - Execute few lines of code when received silent push notification and app is running in background, ios 让iOS应用程序在前台运行 - Keep an iOS app running in foreground 当应用程序收到通知时,IOS 向 HTTP 服务器发送请求 - IOS send request to HTTP server when app received notification 在前台应用程序时获取推送通知 iOS - Get push notification while App in foreground iOS 当我收到推送通知时如何播放声音(应用正在运行时) - How to play sound when i received push notification(when app is running) NSLocalNotification - 在iOS5中运行应用程序时显示通知动画 - NSLocalNotification - show notification animation when the app is running in iOS5 iOS-应用程序运行时不显示推送通知警报 - iOS - Push notification alert is not shown when the app is running 如何确定iOS上哪些应用程序为背景,哪些应用程序为前景 - how to determine which apps are background and which app is foreground on iOS 即使应用未在前台运行,我们也可以定期获取IOS中的位置更新吗? - can we get location updates in IOS swift in a regular intervel of time even when app is not running in foreground 收到推送通知后,如何判断我的iPhone应用程序是否正在运行? - How do I tell if my iPhone app is running when a Push Notification is received?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM