在iOS中使用本地通知设置提醒目标C

时间:2016-12-25 13:05:06

标签: ios objective-c notifications uilocalnotification

这是我的本地通知代码,我每天都会使用[localNotification setRepeatInterval:NSCalendarUnitDay];收到通知我现在想要在使用结束日期和时间的日期和时间到期后停止或取消通知。我应该在哪些方面实施,任何人都请解释。感谢你们。

NSString *startdate = @"10-12-2016 07:00 am";
NSString *enddate = @"14-12-2016 07:00 am";
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = startdate;
localNotification.alertBody = [NSString stringWithFormat:@"%@ ",self.nameTextField.text];
            localNotification.soundName = @"bell_tree.mp3";
            localNotification.alertAction = @"Show me the item";
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
    [localNotification setRepeatInterval:NSCalendarUnitDay];
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

这是我的app委托didReceiveLocalNotification编码,

- (void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification {
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder"
                                                        message:notification.alertBody
                                                       delegate:self cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }

    // Request to reload table view data
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];

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

}

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码取消该通知:

使用以下代码行取消一个本地通知:[[UIApplication sharedApplication] cancelLocalNotification:theNotification]

您可以在向用户显示通知后在didReceiveLocalNotification中执行此操作..或者将"notification"对象传递给该类/方法,无论用户是否取消它。

或者您可以实施“取消”按钮点击提醒,然后在该点击上您可以取消该提醒(localNotification)。