我每30秒初始化一次本地通知。我想在用户按下按钮停止本地通知后停止重复。
问题是我找不到办法。它每30秒重复一次
这就是我教育本地化的方法
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:30];
localNotification.alertBody = @"Testing Repeating Local Notification";
localNotification.applicationIconBadgeNumber = 1;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.repeatCalendar = [NSCalendar currentCalendar];
localNotification.repeatInterval = kCFCalendarUnitSecond;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
我试过了[[UIApplication sharedApplication] cancelAllLocalNotifications];
。但它很有效。
答案 0 :(得分:1)
您可以将repeatInterval
设置为0.根据文档,如果设置为零,通知将被触发一次。因此,当按下停止按钮时,您可以执行以下操作
localNotification.repeatInterval = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
答案 1 :(得分:0)
我解决了。
刚刚从上面的代码中删除了localNotification.repeatCalendar = [NSCalendar currentCalendar];
。
答案 2 :(得分:0)
您可以在应用程序处于活动状态时收到通知时删除通知。
就像
一样- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if (application.applicationState == UIApplicationStateActive)
{
NSLog(@"Application is active");
/*
...
do some process
...
*/
//remove notification
[application cancelLocalNotification:notification];
}
else
{
NSLog(@"Application is inactive");
}
}