简体   繁体   中英

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. 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:

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

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