收到推送通知时如何判断我的 iPhone 应用程序是否正在运行?
我正在向我的 iPhone 应用程序发送推送通知,并且我希望根据应用程序是否已启动来执行一组不同的指令。我是 iPhone 开发新手,虽然我怀疑 UIApplication 或我项目的 AppDelegate 类有解决方案,但我还没有找到好的答案。有没有一种简单的方法可以检查这一点?
I am sending Push Notifications to my iPhone app, and I'd like a different set of instructions to execute depending on whether the app is already launched or not. I'm new to iPhone development, and while I suspect UIApplication or my project's AppDelegate class has the solution, I haven't found a good answer. Is there an easy way to check for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
didReceiveRemoteNotification:
在应用程序运行时调用,是的,但是当它挂起时,iOS
会负责放置徽章等。如果应用程序位于在前台,操作系统不执行任何操作,只是调用您的didReceiveRemoteNotification:
。didReceiveRemoteNotification:
is called when the app is running, yes, but when it is suspended, theiOS
takes care of putting up the badge, etc. If the app is in the foreground, the OS does nothing, and just calls yourdidReceiveRemoteNotification:
.根据您所说的“启动”的含义,您要么在寻找:
使用一个在以下情况下设置为 true 的标志:应用程序变为活动状态,当应用程序不活动时为 false。
标志(在头文件 [.h] 中):
代码(在实现文件 [.m] 中):
这是有效的,因为当应用程序挂起时,操作系统在“唤醒”期间调用“applicationDidBecomeActive”之前调用“applicationDidReceiveRemoteNotification”过程。
“完整”的答案实际上是凯文的答案加上这个答案。
希望这有帮助。
Depending upon what you mean by "launched", you are either looking for:
Use a flag that is set true when the application becomes active, and false when the application is not active.
Flag (in header file [.h]):
Code (in implementation file [.m]):
This works because when the application is suspended, the OS calls "applicationDidReceiveRemoteNotification" before it calls "applicationDidBecomeActive" during the "wake-up" process.
The "complete" answer is actually Kevin's answer plus this answer.
Hope this helps.
方法
UIApplication 委托具有您需要实现的 。当应用程序运行时,它会收到通知。
如果您的应用程序当前未运行并且收到通知,则可以使用
launchOptions 字典中保存的通知详细信息启动您的应用程序。如果字典为零,则用户照常点击应用程序图标。
The UIApplication delegate has the method
which you need to implement. This receives the notification when the app is running.
If your app is not currently running and a notification is received then your app can be launched with
with the notification details held in the launchOptions dictionary. if the dictionary is nil then the user tapped the application icon as normal.
如果您要在小于 4 的 iOS 上检查 applicationState,则需要检查 applicationState 是否受支持:
If you are going to check applicationState on iOS less than 4, you'll need to check that applicationState is supported:
Apple 的推送通知文档对此进行了解释:
这意味着,如果您的 application:didReceiveRemoteNotification: 委托方法被调用,则您的应用程序正在运行。
The Apple documentation for push notifications explains this:
This means that if your application:didReceiveRemoteNotification: delegate method is called, your app is running.