【问题标题】:Showing Local Notifications using Location Manager, While app is in the background使用位置管理器显示本地通知,而应用程序在后台
【发布时间】:2012-02-15 02:13:35
【问题描述】:

我是 iPhone 编程的新手。我开发了一个应用程序来检查用户进入特定区域。但我需要在后台检查。在后台我正在检查但问题是重复 UILocalNotification 警报。 那么如何防止重复的 UILocalNotifications

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    NSLog(@"running in background ...");

    [self checkRegionEntered];

    CurrentlattitudeValue1 =newLocation.coordinate.latitude;
    CurrentlongitudeValue1=newLocation.coordinate.longitude;
}

-(void)checkRegionEntered
{

    if ([testRegion containsCoordinate:currentCoordinates]) 
    {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        Class cls = NSClassFromString(@"UILocalNotification");
        if (cls != nil) 
        {
            UILocalNotification *notif = [[cls alloc] init];
            NSDate *now = [NSDate date];
            [notif setFireDate:now];

            if([Obj.NotesGeo length])
                [notif setAlertBody:Obj.NotesGeo];
            else 
            {
                [notif setAlertBody:[NSString stringWithFormat:@", you have arrived at %@",Obj.NameGeo]];
            }

            [notif setAlertAction:@"Launch"];
            notif.soundName=[NSString stringWithFormat:@"%@.wav",Obj.Ringtone1];//[NSString stringWithFormat:@"%g",Obj.LatitudeGeo]
            NSDictionary *userDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%f",Obj.LatitudeGeo] forKey:kRemindMeNotificationDataKey];

            notif.userInfo = userDict;
        }
    }
}

【问题讨论】:

  • in -(void)checkRegionEntered 我正在显示 localNotification 之类的

标签: iphone objective-c core-location cllocationmanager


【解决方案1】:

这可能会有所帮助。此逻辑旨在确保您的通知只会触发一次,并且无论应用在触发时是在前台还是在后台,都会显示相同的警报。

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

    // Add the view controller's view to the window and display.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    //detect if app was launched by notification
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (notification)
    {
        //trigger our custom notification handler
        [self handleLocalNotification:notification];
    }

    return YES;
}

- (void)handleLocalNotification:(UILocalNotification *)notification
{
    //this is our custom method to handle the in-app notification behaviour
    [[[[UIAlertView alloc] initWithTitle:@"You have a notification"
                                 message:@"Yay!"
                                delegate:nil
                       cancelButtonTitle:@"OK"
                       otherButtonTitles:nil] autorelease] show];
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    //this standard application delegate method is called under the following circumstances
    //1. the app is running in the foreground when the notification fires
    //2. the app was running in the background when the notification fired, and the user pressed the action button
    //confusingly, if the app was not running when the notification fired, this method won't be called at startup
    //automatically and you need to check for the notification key in the launch options instead

    if ([UIApplication sharedApplication].applicationState != UIApplicationStateBackground)
    {
        //this code emulates the same behaviour as when a local notification is received
        //when the app is not running (except for playing the sound)

        //store notification temporarily
        [lastNotification release];
        lastNotification = [notification retain];

        //get button labels
        NSString *actionButton = nil;
        NSString *cancelButton = @"Close";
        if (notification.hasAction)
        {
            actionButton = (notification.alertAction)? notification.alertAction: @"View"; //defaults to "View" if nil
            cancelButton = @"OK";
        }

        //show alert
        [[[[UIAlertView alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"] //name of application
                                     message:notification.alertBody
                                    delegate:self
                           cancelButtonTitle:cancelButton
                           otherButtonTitles:actionButton, nil] autorelease] show];
    }
    else
    {
        //trigger our custom notification handler
        [self handleLocalNotification:notification];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != alertView.cancelButtonIndex)
    {
        //trigger our custom notification handler
        [self handleLocalNotification:lastNotification];
    }
}

【讨论】:

  • 您无需创建 BOOL 来跟踪您是否有背景。您只需检查applicationState 即可查看您是否在后台运行。
  • 好电话 - 我已更新代码示例以改用 applicationState。
  • 它更好地检查应用程序状态是否 UIApplicationStateActive。如果是的话,你的其他东西就别这样了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-08
  • 1970-01-01
  • 2020-10-23
相关资源
最近更新 更多