从处于活动状态的didReceiveLocalNotification中删除警报视图后,前台没有通知

时间:2016-03-09 07:08:40

标签: ios objective-c notifications

我正在实施本地通知,一天中的不同时间有3个通知要发送。 在我的应用代表

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
}

和DidReceiveLocalNotifications

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

NSString *checkbtn= [[NSUserDefaults standardUserDefaults]valueForKey:@"on"];

    if([checkbtn isEqualToString:@"SwitchOn"])
    {
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Green Actions"
                                                        message:notification.alertBody
                                                       delegate:self cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }


    }

    // Set icon badge number to zero
    application.applicationIconBadgeNumber = 0;
//}

}

如果我删除此警报视图,我在前台没有收到任何通知,但如果我使用它,那么我每秒都会收到通知警报。

这是我的viewcontroller代码

- (void)viewDidLoad {
    [super viewDidLoad];
    appdelegate= (AppDelegate *) [[UIApplication sharedApplication]delegate];
    [self createCustomeNavigationBar];


    self.navigationController.navigationBar.translucent = NO;
    //[self notificationOne];


    [self.switchbutton addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
    [self.switchbutton1 addTarget:self action:@selector(switchToggled1:) forControlEvents:UIControlEventValueChanged];
    [self.switchbutton3 addTarget:self action:@selector(switchToggled3:) forControlEvents:UIControlEventValueChanged];

     if ([[NSUserDefaults standardUserDefaults]valueForKey:@"on"])
     {

    UILocalNotification* n1 = [[UILocalNotification alloc] init];
    n1.fireDate = [NSDate dateWithTimeIntervalSinceNow: 160];
    n1.alertBody = @"one";
         [[UIApplication sharedApplication] scheduleLocalNotification: n1];
     }
    if ([[NSUserDefaults standardUserDefaults]valueForKey:@"SwitchOneon"])
    {
    UILocalNotification* n2 = [[UILocalNotification alloc] init];
    n2.fireDate = [NSDate dateWithTimeIntervalSinceNow: 190];
    n2.alertBody = @"two";
        [[UIApplication sharedApplication] scheduleLocalNotification: n2];
    }


    // Do any additional setup after loading the view from its nib.
}
- (void) switchToggled:(id)sender {
    self.switchbutton = (UISwitch *)sender;
    if ([self.switchbutton   isOn]) {
        NSLog(@"its on!");

        [[NSUserDefaults standardUserDefaults]setObject:@"SwitchOn" forKey:@"on"];
        [[NSUserDefaults standardUserDefaults]synchronize];



    } else {

        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"on"];

        NSLog(@"its off!");

    }
}


- (void) switchToggled1:(id)sender {
    self.switchbutton1 = (UISwitch *)sender;
    if ([self.switchbutton1   isOn]) {
        NSLog(@"its on!");

        [[NSUserDefaults standardUserDefaults]setObject:@"SwitchOn" forKey:@"SwitchOneon"];
        [[NSUserDefaults standardUserDefaults]synchronize];



    } else {

        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"SwitchOneon"];

        NSLog(@"its off!");

    }
}

- (void) switchToggled3:(id)sender {
    self.switchbutton3 = (UISwitch *)sender;
    if ([self.switchbutton3   isOn]) {
        NSLog(@"its on!");

        [[NSUserDefaults standardUserDefaults]setObject:@"SwitchOn" forKey:@"SwitchThreeon"];
        [[NSUserDefaults standardUserDefaults]synchronize];



    } else {

        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"SwitchThreeon"];

        NSLog(@"its off!");

    }
}

并且我在警报视图中获得一些不同的文本,我之前用于测试目的,即使在更改之后,我也会获得相同的旧文本。

1 个答案:

答案 0 :(得分:0)

我确信问题不在于警报代码。 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification,您正在收到通知。但是,当您的应用处于前台(或处于活动状态)时,您应该手动处理它。在您的情况下,您可以使用UIAlertView对其进行处理,以便在警报中显示,但是一旦删除,当您的应用处于前台(或处于活动状态)时,您就无法看到通知)状态。