简体   繁体   English

应用终止后无法处理本地通知

[英]Unable to Handle Local Notification when app has been terminated

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 我制作的应用程序会跟踪目标和里程碑(属于目标),并通过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. 当应用程序处于后台状态且应用程序处于前台时,我的应用程序已成功处理了本地通知。

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. 我知道,如果您希望处理终止应用程序时收到的本地通知,则需要通过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: 但是,每当我测试应用程序并在应用程序终止后激活本地通知时,我都无法在application:didFinishLaunchingWithOptions中处理本地通知:

I suspect it has something to do with either launch options being nil or launch options not having the local notification payload. 我怀疑这与启动选项为nil或启动选项没有本地通知有效负载有关。

The way I test this scenario is as such: 我测试这种情况的方式是这样的:

  1. I build and run the app (command + R) 我构建并运行该应用程序(命令+ 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 我停止运行模拟器,然后将Mac的时间更改为开火日期之前
  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) 我再次构建并运行该应用程序,然后继续从模拟器中终止该应用程序(通过Shift + Command + H两次,然后向上滑动)
  5. I lock the screen and wait for the notification 我锁定屏幕并等待通知
  6. After it fires I swipe the notification on the lock screen 触发后,我在锁定屏幕上滑动通知

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. 通知触发并刷卡通知后,将启动该应用程序,但是我在处理application:didFinishLaunchingWithOptions:中的本地通知有效负载时所使用的方法均未调用。

My Code in my application:didFinishLaunchingWithOptions: to handle the local notification payload is as follows: 我在我的应用程序中的代码:didFinishLaunchingWithOptions:处理本地通知有效内容如下:

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

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

My Code in my application:didReceiveLocalNotification: is as follows: 我的应用程序中的代码: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
    }


}

as well as the helper method performFollowUpActionForNotification: 以及辅助方法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. 希望对您有帮助,请在下面查看。

In iOS 7.1 Apple has introduced a very important update to how the system handles notifications triggered by beacons. 在iOS 7.1中,Apple对系统如何处理由信标触发的通知进行了非常重要的更新。 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. 与iOS 7相比,这是一个了不起的改进,但是关于它的实际工作方式仍然存在很多困惑,因此我们准备了一个简短的教程。

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. 在应用终止时,电话每次进入或退出信标区域,都会自动启动,并使用launchOptions参数中存在的UIApplicationLaunchOptionsLocationKey键调用application:didFinishLaunchingWithOptions:方法(属于AppDelegate类)。

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. 当您确认此密钥存在(因此位置是启动应用的原因)时,应创建ESTBeaconManager类的新实例,将委托设置为AppDelegate对象(或任何其他充当ESTBeaconManagerDelegate并在此事件发生之前创建的对象)并开始监视。 Region you are passing to the startMonitoringForRegion: method is not important, as ESTBeaconManager delegate will receive most recent region information. 您传递给startMonitoringForRegion:方法的区域并不重要,因为ESTBeaconManager委托将接收最新的区域信息。 You can just pick any of the ones your app registered in iOS. 您可以选择您的应用在iOS中注册的任何应用。 When the monitoring was revoked, app will automatically receive most recent entered/exited region event in beaconManager:didEnterRegion: or beaconManager:didExitRegion: method. 撤销监视后,应用程序将在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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序终止时处理点击本地通知 - iOS - Swift - Handle tap on local notification when app is terminated - iOS - Swift 应用终止后删除本地通知 - Removing Local Notifications when app has been terminated 在应用终止时安排本地通知 - Scheduling a local notification when the app is terminated 用户终止应用程序时发出iBeacon通知(通过在任务视图中向上滑动它) - iBeacon Notification when the app has been terminated by user (by swiping it up in the task view) 终止后,通过推送通知启动应用程序 - Launch an app with push notification after it has been terminated 应用终止后,使用NSNotification在UIViewController中处理UILocalNotification - Handle UILocalNotification in UIViewController using NSNotification after the app has been terminated 应用终止后发送本地通知 - Sending local notifications after the app has been terminated 应用未运行/应用终止时如何处理推送通知 - How to handle push notification when app is not running/app is terminated iOS Swift 应用终止时处理通知点击 - iOS Swift Handle notification click when app is terminated 当应用程序在后台时处理本地通知 - Handle local notification when app is in background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM