ios - 应用程序终止时无法处理本地通知

标签 ios iphone xcode swift notifications

我制作的应用程序跟踪目标和里程碑(属于目标)并在用户即将达到目标或里程碑的截止日期时通过 UILocalNotification 提醒用户

当应用处于后台状态和处于前台时,我的应用已成功处理本地通知。

我知道如果你想在应用程序终止时处理收到的本地通知,你需要通过应用程序的启动选项访问本地通知:didFinishLaunchingWithOptions: appDelegate 中的方法。

但是,每当我测试应用程序并在应用程序终止后激活本地通知时,我无法在 application:didFinishLaunchingWithOptions 中处理本地通知:

我怀疑这与启动选项为零或启动选项没有本地通知负载有关。

我测试这个场景的方式是这样的:

  1. 我构建并运行应用程序(command + R)
  2. 我安排通知
  3. 我停止运行模拟器,然后将 mac 的时间更改为 在点火日期之前
  4. 我再次构建并运行该应用程序,然后继续终止该应用程序 从模拟器(通过 shift + command + h 两次然后 向上滑动)
  5. 我锁屏等待通知
  6. 触发后,我在锁定屏幕上滑动通知

在通知触发并且我滑动通知后,应用程序启动但我在 application:didFinishLaunchingWithOptions: 中用于处理本地通知负载的方法没有被调用。

我的应用程序中的代码:didFinishLaunchingWithOptions:处理本地通知负载如下:

 if let options = launchOptions {
        let value = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification

        if let notification = value {
            self.application(application, didReceiveLocalNotification: notification)
        }
    }

我的application:didReceiveLocalNotification:中的代码如下:

    func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    //If the user receives a notification while app is in the foreground
    println("")

    if application.applicationState == UIApplicationState.Active {
        println("did receive notification")
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
        let alertController = UIAlertController(title: nil, message: notification.alertBody!, preferredStyle: UIAlertControllerStyle.Alert)

        let alertAction = UIAlertAction(title: "View", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
        self.performFollowUpActionForNotification(notification)
        }

        let cancelAction = UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler: nil)

        alertController.addAction(alertAction)
        alertController.addAction(cancelAction)

        self.window!.rootViewController!.presentViewController(alertController, animated: true, completion: nil)
    }

    else {
        performFollowUpActionForNotification(notification)
        application.applicationIconBadgeNumber = 0
    }


}

以及辅助方法 performFollowUpActionForNotification:

func performFollowUpActionForNotification(notification:UILocalNotification) {

    if notification.alertAction! == "View Goal" {
        if let tabVC = self.window?.rootViewController? as? UITabBarController {
            let navCon = tabVC.viewControllers![0] as UINavigationController
            if navCon.topViewController is GoalPageViewController {

            }

            else {
                navCon.pushViewController(navCon.viewControllers![0] as UIViewController, animated: true )

            }
        }
    }

    if notification.alertAction! == "View Milestone" {
        if let tabVC = self.window?.rootViewController? as? UITabBarController {

            let navCon = tabVC.viewControllers![0] as UINavigationController
            if let goalPageVC = navCon.viewControllers![0] as? GoalPageViewController {
                goalPageVC.findGoalThatContainsMilestoneAndGoToDetailForNotification(notification)
            }
            else {println("cant down cast view controller to goal page view controller")}
        }
    }
}

是我的代码有问题还是我如何针对这种情况测试我的应用程序?我迫切需要一个答案。

最佳答案

希望这对您有所帮助,请看下面。

在 iOS 7.1 中,Apple 对系统处理信标触发的通知的方式进行了非常重要的更新。现在,应用程序可以在进入/退出监控事件发生时采取行动,即使它已被终止。与 iOS 7 相比,这是一个惊人的改进,但对于它的实际工作方式仍然存在很多困惑,因此我们准备了一个简短的教程。

位置事件(在本例中与信标相关)的处理方式与任何其他应用程序启动事件的处理方式相同。应用程序终止时,每次手机进入或退出信标区域时,它都会自动启动并调用 application:didFinishLaunchingWithOptions: 方法(AppDelegate 类),使用 launchOptions 参数中存在的 UIApplicationLaunchOptionsLocationKey 键调用。

当您验证此键存在时(所以位置是您的应用程序启动的原因)您应该创建 ESTBeaconManager 类的新实例,将委托(delegate)设置为 AppDelegate 对象(或作为 ESTBeaconManagerDelegate 工作并在此之前创建的任何其他对象事件发生)并开始监控。您传递给 startMonitoringForRegion: 方法的区域并不重要,因为 ESTBeaconManager 委托(delegate)将接收最新的区域信息。您可以选择您在 iOS 中注册的应用程序中的任何一个。当监听被撤销时,应用会自动接收最近进入/退出的区域事件在beaconManager:didEnterRegion: 或beaconManager:didExitRegion: 方法中。

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

{ 

   if([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"])
   {
    self.beaconManager = [ESTBeaconManager new];
    self.beaconManager.delegate = self;
    // don't forget the NSLocationAlwaysUsageDescription in your Info.plist
    [self.beaconManager requestAlwaysAuthorization];
    [self.beaconManager startMonitoringForRegion:[[ESTBeaconRegion alloc]
                                                  initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID
                                                  identifier:@"AppRegion"]];

    }

   return YES;

    }

 -(void)beaconManager:(ESTBeaconManager *)manager didEnterRegion:(ESTBeaconRegion *)region

{

 UILocalNotification *notification = [[UILocalNotification alloc] init];
 notification.alertBody = @"Enter region";
 notification.soundName = UILocalNotificationDefaultSoundName;

 [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

}

-(void)beaconManager:(ESTBeaconManager *)manager didExitRegion:(ESTBeaconRegion *)region
{
  UILocalNotification *notification = [[UILocalNotification alloc] init];
  notification.alertBody = @"Exit region";
  notification.soundName = UILocalNotificationDefaultSoundName;

 [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

关于ios - 应用程序终止时无法处理本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27900876/

相关文章:

iphone - 在 iPhone 游戏中处理图形的最佳方式

ios - 如何在编译时检查当前项目中是否存在 Swift 模块?

c - 将应用程序预处理器信息转发到静态库

swift - 当我尝试获取 geocodeAddressString 时,它不断抛出此错误

android - FlashBuilder Away3D iOS -

iphone - 设置 UITabBarControllers 的 selectedIndex 属性后禁用自动旋转(SDK 错误?)

iphone - 在具有多个变量的另一个类中调用方法的正确语法是什么?

ios - 加载核心数据获取请求的结果集

iphone - Bing Api 不适合我

ios - 想要将数据传递到其他组件-ListView