收到推送通知时如何判断我的 iPhone 应用程序是否正在运行?

发布于 2024-08-16 16:53:44 字数 149 浏览 5 评论 0原文

我正在向我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

随心而道 2024-08-23 16:53:44

这是处理应用程序的活动/非活动状态的更合适的方法。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {   
        // check for the app state
        UIApplicationState state = [application applicationState];

        if (state == UIApplicationStateActive) {
            //the app is in the foreground, so here you do your stuff since the OS does not do it for you
            //navigate the "aps" dictionary looking for "loc-args" and "loc-key", for example, or your personal payload)
        }

    application.applicationIconBadgeNumber = 0;
}

didReceiveRemoteNotification: 在应用程序运行时调用,是的,但是当它挂起时,iOS 会负责放置徽章等。如果应用程序位于在前台,操作系统不执行任何操作,只是调用您的 didReceiveRemoteNotification:

Here's the more appropriate way of handling active/inactive state of the app.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {   
        // check for the app state
        UIApplicationState state = [application applicationState];

        if (state == UIApplicationStateActive) {
            //the app is in the foreground, so here you do your stuff since the OS does not do it for you
            //navigate the "aps" dictionary looking for "loc-args" and "loc-key", for example, or your personal payload)
        }

    application.applicationIconBadgeNumber = 0;
}

didReceiveRemoteNotification: is called when the app is running, yes, but when it is suspended, the iOS takes care of putting up the badge, etc. If the app is in the foreground, the OS does nothing, and just calls your didReceiveRemoteNotification:.

青春有你 2024-08-23 16:53:44

根据您所说的“启动”的含义,您要么在寻找:

  • 上面凯文的答案(区分已启动或未启动),
  • 要么是这个(区分已暂停或活动,但已启动):

使用一个在以下情况下设置为 true 的标志:应用程序变为活动状态,当应用程序不活动时为 false。

标志(在头文件 [.h] 中):

BOOL applicationIsActive;

代码(在实现文件 [.m] 中):

- (void)applicationDidBecomeActive:(UIApplication *)application {
    applicationIsActive = YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    applicationIsActive = NO;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if (applicationIsActive) {
        // Handle notification in app active state here
    }
    else {
        // Handle notification in app suspended state here
    }

这是有效的,因为当应用程序挂起时,操作系统在“唤醒”期间调用“applicationDidBecomeActive”之前调用“applicationDidReceiveRemoteNotification”过程。

“完整”的答案实际上是凯文的答案加上这个答案。

希望这有帮助。

Depending upon what you mean by "launched", you are either looking for:

  • Kevin's answer above (differentiates between launched or not launched)
  • or this (differentiates between suspended or active, but already launched):

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]):

BOOL applicationIsActive;

Code (in implementation file [.m]):

- (void)applicationDidBecomeActive:(UIApplication *)application {
    applicationIsActive = YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    applicationIsActive = NO;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if (applicationIsActive) {
        // Handle notification in app active state here
    }
    else {
        // Handle notification in app suspended state here
    }

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.

勿忘初心 2024-08-23 16:53:44

方法

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

UIApplication 委托具有您需要实现的 。当应用程序运行时,它会收到通知。

如果您的应用程序当前未运行并且收到通知,则可以使用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

launchOptions 字典中保存的通知详细信息启动您的应用程序。如果字典为零,则用户照常点击应用程序图标。

The UIApplication delegate has the method

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

with the notification details held in the launchOptions dictionary. if the dictionary is nil then the user tapped the application icon as normal.

谁与争疯 2024-08-23 16:53:44

如果您要在小于 4 的 iOS 上检查 applicationState,则需要检查 applicationState 是否受支持:

if ([application respondsToSelector:@selector(applicationState)] ){
  // Safe to check applicationState
  UIApplicationState state = [application applicationState];
}

If you are going to check applicationState on iOS less than 4, you'll need to check that applicationState is supported:

if ([application respondsToSelector:@selector(applicationState)] ){
  // Safe to check applicationState
  UIApplicationState state = [application applicationState];
}
阳光下的泡沫是彩色的 2024-08-23 16:53:44

Apple 的推送通知文档对此进行了解释:

但是,有两种情况 applicationDidFinishLaunching: 不是合适的实现站点:

  • 通知到达时应用程序正在运行。
  • 通知负载包含应用程序可以使用的自定义数据。

在第一种情况下,当iPhone OS收到远程通知时应用程序正在运行,如果您想立即下载数据,您应该实现UIApplicationDelegate的application:didReceiveRemoteNotification:方法。下载后,请务必删除应用程序图标上的徽章。 (如果您的应用程序经常与其提供者检查新数据,则可能不需要实现此方法。)

这意味着,如果您的 application:didReceiveRemoteNotification: 委托方法被调用,则您的应用程序正在运行。

The Apple documentation for push notifications explains this:

However, there are two situations where applicationDidFinishLaunching: is not a suitable implementation site:

  • The application is running when the notification arrives.
  • The notification payload contains custom data that the application can use.

In the first case, where the application is running when iPhone OS receives a remote notification, you should implement the application:didReceiveRemoteNotification: method of UIApplicationDelegate if you want to download the data immediately. After downloading, be sure to remove the badge from the application icon. (If your application frequently checks with its provider for new data, implementing this method might not be necessary.)

This means that if your application:didReceiveRemoteNotification: delegate method is called, your app is running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文