【问题标题】:How to determine background app come to foreground in action of tapping local notification如何确定后台应用程序在点击本地通知时进入前台
【发布时间】:2016-10-22 11:53:22
【问题描述】:

苹果文档Getting the User’s Attention While in the Background

通知是暂停应用的一种方式,它位于 背景,或者没有运行以引起用户的注意。

由于区域监控和is in the background,我的应用被 iOS 唤醒并发布本地通知。用户点击通知,应用程序将在前台。

由于用户点击了通知,如何确定应用进入前台?

哪个委托方法将包含通知信息。

didFinishLaunchingWithOption or didReceiveLocalNotification

【问题讨论】:

  • 如果应用程序被挂起并且没有运行,那么应用程序将调用“didFinishLaunchingWithOption”,如果应用程序在后台运行,那么您将在“didReceiveLocalNotification”中收到通知信息。

标签: ios objective-c appdelegate region-monitoring


【解决方案1】:

如果您的应用程序在后台运行并且您在 LocalNotification Banner 上被点击,那么您将被调用以下方法:

-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

iOS 8 之后:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler

如果应用程序未在后台运行,您将在以下位置收到通知:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if ([launchOptions valueForKey:@"UIApplicationLaunchOptionsLocalNotificationKey"]) {
 // here you will get
}

【讨论】:

    【解决方案2】:

    当 UILocalNotification 被触发时,您可以检测您的应用程序的状态是什么,如果 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 被调用,这确保接收到本地通知。

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
        UIApplicationState state = [application applicationState];
        if (state == UIApplicationStateInactive) {
            // Application was in the background when notification was delivered.
        } else {
    
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以通过使用 application.applicationState 来确定这一点

       if(application.applicationState == UIApplicationStateInactive)
           {
                 NSLog(@" Inactive");
            // when you tapping on notification
      
           }
          else if (application.applicationState == UIApplicationStateBackground)
          {
                 NSLog(@" background");
          }
          else
          {
                NSLog(@" Active");
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-07
        • 1970-01-01
        • 1970-01-01
        • 2021-09-08
        相关资源
        最近更新 更多