应用终止后无法处理本地通知 [英] Unable to Handle Local Notification when app has been terminated

查看:60
本文介绍了应用终止后无法处理本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The app that I have made keeps track of Goals and Milestones(which belong to a goal) and reminds the user when they are about to reach the due date of a goal or a milestone through a UILocalNotification

当我的应用程序处于后台状态并且处于前台状态时,我的应用程序已成功处理了本地通知.

my app has been successful in handling local notifications when the app is in a background state and when the app is in the foreground.

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

I am aware that if you want to handle local notifications when they are received when the app is terminated you are required to access the local notification through the launch options of the application:didFinishLaunchingWithOptions: method in your appDelegate.

但是,无论何时我测试应用程序并在应用程序终止后激活本地通知,我都无法在application:didFinishLaunchingWithOptions:

However whenever I test the app and activate the local notification after the app has terminated, I am unable to handle the local notification in application:didFinishLaunchingWithOptions:

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

I suspect it has something to do with either launch options being nil or launch options not having the local notification payload.

我测试这种情况的方式是这样的:

The way I test this scenario is as such:

  1. 我构建并运行该应用程序(命令+ R)
  2. 我安排了通知
  3. 我停止运行模拟器,然后将Mac的时间更改为 在开火日期之前
  4. 我再次构建并运行该应用程序,然后继续终止该应用程序 从模拟器(通过Shift + Command + h两次,然后 向上滑动)
  5. 我锁定屏幕并等待通知
  6. 启动后,我在锁定屏幕上滑动通知
  1. I build and run the app (command + R)
  2. I schedule the notification
  3. I stop running the simulator and then change my mac's time to just before the fire date
  4. I build and run the app again and then proceed to terminate the app from the simulator (by shift + command + h twice and then swiping up)
  5. I lock the screen and wait for the notification
  6. After it fires I swipe the notification on the lock screen

在触发通知并刷卡通知后,该应用程序将启动,但我在处理application:didFinishLaunchingWithOptions:中的本地通知有效内容时所使用的方法均未被调用.

After the notification fires and i swipe the notification, the app is launched but the method that i used in handling the local notification payload in application:didFinishLaunchingWithOptions: is not called whatsoever.

我在我的应用程序中的代码:didFinishLaunchingWithOptions:处理本地通知有效内容的方法如下:

My Code in my application:didFinishLaunchingWithOptions: to handle the local notification payload is as follows:

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

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

我的应用程序中的代码:didReceiveLocalNotification:如下:

My Code in my application:didReceiveLocalNotification: is as follows:

    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:

as well as the helper method 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")}
        }
    }
}

我的代码是否有问题,或者在这种情况下如何测试我的应用?我迫切需要一个答案.

Is the problem with my code or how i test my app for this scenario? I am desperately in need of an answer.

推荐答案

希望对您有所帮助,请在下面查看.

Hope this will be helpful for you please have look below.

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

In iOS 7.1 Apple has introduced a very important update to how the system handles notifications triggered by beacons. Now an app can take action when enter/exit monitoring events occur, even if it has been terminated. It’s an amazing improvement in comparison to iOS 7, but there’s still a lot of confusion regarding how it really works, so we’ve prepared a short tutorial.

位置事件(在这种情况下与信标有关)的处理方式与任何其他应用程序启动事件相同.在应用终止时,电话每次进入或退出信标区域,都会自动启动,并使用launchOptions参数中存在的UIApplicationLaunchOptionsLocationKey键调用application:didFinishLaunchingWithOptions:方法(属于AppDelegate类).

Location events (related to the beacons in this case) are handled the same way as any other app launching events. Every single time phone enters or exits beacon’s region while the app is terminated, it will be automatically launched and application:didFinishLaunchingWithOptions: method (of AppDelegate class) is called with UIApplicationLaunchOptionsLocationKey key existing in launchOptions parameter.

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

When you verify this key exists (so location was the reason that your app was launched) you should create new instance of ESTBeaconManager class, set delegate to AppDelegate object (or any other object that is working as ESTBeaconManagerDelegate and was created before this event occurred) and start monitoring. Region you are passing to the startMonitoringForRegion: method is not important, as ESTBeaconManager delegate will receive most recent region information. You can just pick any of the ones your app registered in iOS. When the monitoring was revoked, app will automatically receive most recent entered/exited region event in beaconManager:didEnterRegion: or beaconManager:didExitRegion: method.

-(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];
}

这篇关于应用终止后无法处理本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆