Ios 应用程序终止时无法处理本地通知
我制作的应用程序跟踪目标和里程碑(属于目标),并通过UILocalNotification提醒用户何时即将到达目标或里程碑的截止日期 当应用程序处于后台状态和前台状态时,我的应用程序已成功处理本地通知 我知道,如果您希望在应用程序终止时接收本地通知时处理这些通知,则需要通过appDelegate中application:didFinishLaunchingWithOptions:method的启动选项访问本地通知 但是,每当我测试应用程序并在应用程序终止后激活本地通知时,我无法在application:didFinishLaunchingWithOptions: 我怀疑这与启动选项为零或启动选项没有本地通知有效负载有关 我测试此场景的方法如下所示:Ios 应用程序终止时无法处理本地通知,ios,iphone,xcode,swift,notifications,Ios,Iphone,Xcode,Swift,Notifications,我制作的应用程序跟踪目标和里程碑(属于目标),并通过UILocalNotification提醒用户何时即将到达目标或里程碑的截止日期 当应用程序处于后台状态和前台状态时,我的应用程序已成功处理本地通知 我知道,如果您希望在应用程序终止时接收本地通知时处理这些通知,则需要通过appDelegate中application:didFinishLaunchingWithOptions:method的启动选项访问本地通知 但是,每当我测试应用程序并在应用程序终止后激活本地通知时,我无法在applicat
if let options = launchOptions {
let value = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification
if let notification = value {
self.application(application, didReceiveLocalNotification: notification)
}
}
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
}
}
我的应用程序:didReceiveLocalNotification:中的代码如下:
if let options = launchOptions {
let value = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification
if let notification = value {
self.application(application, didReceiveLocalNotification: notification)
}
}
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
}
}
以及helper方法执行以下通知操作:
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中,苹果对系统如何处理信标触发的通知进行了非常重要的更新。现在,应用程序可以在发生进入/退出监视事件时采取操作,即使它已被终止。与iOS 7相比,这是一个惊人的改进,但是关于它到底是如何工作的,仍然有很多困惑,所以我们准备了一个简短的教程 位置事件(本例中与信标相关)的处理方式与任何其他应用程序启动事件相同。在应用程序终止时,每次手机进入或退出信标区域时,它都会自动启动,并使用launchOptions参数中存在的UIApplicationLaunchOptions键调用application:didFinishLaunchingWithOptions:method(属于AppDelegate类) 当您验证此密钥存在时(因此位置是启动应用程序的原因),您应该创建ESTBeaconManager类的新实例,将委托设置为AppDelegate对象(或在此事件发生之前创建的作为ESTBeaconManagerDelegate工作的任何其他对象),并开始监视。传递给startMonitoringForRegion:方法的区域不重要,因为ESTBeaconManager委托将接收最新的区域信息。你可以选择你的应用在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 7.1中,苹果对系统如何处理信标触发的通知进行了非常重要的更新。现在,应用程序可以在发生进入/退出监视事件时采取操作,即使它已被终止。与iOS 7相比,这是一个惊人的改进,但是关于它到底是如何工作的,仍然有很多困惑,所以我们准备了一个简短的教程 位置事件(本例中与信标相关)的处理方式与任何其他应用程序启动事件相同。在应用程序终止时,每次手机进入或退出信标区域时,它都会自动启动,并使用launchOptions参数中存在的UIApplicationLaunchOptions键调用application:didFinishLaunchingWithOptions:method(属于AppDelegate类) 当您验证此密钥存在时(因此位置是启动应用程序的原因),您应该创建ESTBeaconManager类的新实例,将委托设置为AppDelegate对象(或在此事件发生之前创建的作为ESTBeaconManagerDelegate工作的任何其他对象),并开始监视。传递给startMonitoringForRegion:方法的区域不重要,因为ESTBeaconManager委托将接收最新的区域信息。你可以选择你的应用在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 7.1中,苹果对系统如何处理信标触发的通知进行了非常重要的更新。现在,应用程序可以在发生进入/退出监视事件时采取操作,即使它已被终止。与iOS 7相比,这是一个惊人的改进,但是关于它到底是如何工作的,仍然有很多困惑,所以我们准备了一个简短的教程 位置事件(本例中与信标相关)的处理方式与任何其他应用程序启动事件相同。当应用程序终止时,每次手机进入或退出beacon的区域时,它都会自动关闭