在 iOS 上收到通知时确定应用程序是否正在前台运行
我想找到一种方法来查看哪些应用程序正在前台运行,或者当我的应用程序出现本地通知时是否显示主屏幕。例如,如果主屏幕或其他应用程序中有不同的操作,我希望有不同的操作。 我尝试使用processed和pid,但是pid是在应用程序启动时生成的,而不是最后一次使用应用程序时生成的。 有什么想法吗? 谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如推送通知文档 你可以阅读< code>[[UIApplication sharedApplication] applicationState] 当您收到通知以确定您的应用程序是否处于前台、非活动状态(它是可见的,但像WiFi 选择器位于前面)或位于后台。
As described in the push notification documentation you can read
[[UIApplication sharedApplication] applicationState]
when you receive the notification to determine whether your app is in foreground, inactive (it's visible but a dialog like the WiFi chooser is in front) or in background.只是为了让其他人可以复制粘贴代码:
Just to have a copy-paste code available for others:
Swift 5 版本:
Swift 5 version: