用户是否能够禁用本地通知?

时间:2012-08-24 02:08:41

标签: ios ios5 notifications

我创建了一个应用程序,可以在上午9点向用户发送每日通知,但希望用户能够在需要时将其关闭。

我已为应用设置了设置包,并为enableNotifications设置了一个滑块。当我构建并测试应用程序时,设置就在那里,我可以打开或关闭它们......但它似乎没有在应用程序中注册它。如何对应用程序进行编程以检查通知设置是否开启?

我看到了这个答案:Determine on iPhone if user has enabled push notifications 但我不知道把它放在哪里并正确编程(if / else语句等)

以下是创建通知的代码:

-(id) getCommandInstance:(NSString*)className
{
/** You can catch your own commands here, if you wanted to extend the gap: protocol, or add your
* own app specific protocol to it. -jm
**/
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [NSDate date];

NSDateComponents *dateComponents = [calender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit)
fromDate:currentDate];

NSDateComponents *temp = [[NSDateComponents alloc]init];

[temp setYear:[dateComponents year]];
[temp setMonth:[dateComponents month]];
[temp setDay:[dateComponents day]];
[temp setHour: 22];
[temp setMinute:00];

NSDate *fireTime = [calender dateFromComponents:temp];
[temp release];

// set up the notifier
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = fireTime;
localNotification.timeZone = [NSTimeZone defaultTimeZone];

localNotification.alertBody = @"Don't Forget Your 365 Day Photo Challenge!";
localNotification.alertAction = @"LAUNCH";

localNotification.soundName = UILocalNotificationDefaultSoundName;

localNotification.repeatInterval = NSDayCalendarUnit;
localNotification.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[localNotification release];
return [super getCommandInstance:className];
}

2 个答案:

答案 0 :(得分:1)

如果您只有一个localNotification并想取消它,您可以致电:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

如果没有任何localNotifications,则不会抛出错误。你可以通过按下IBAction按钮来调用它。如果你想根据是否有任何localNotifications设置一个开关的状态,那么你可以检查它是否有任何:

NSArray* localNotifications = [[UIApplication sharedApplication] 
                                              scheduledLocalNotifications];

如果localNotifications.count> 0然后你安排了一些通知。

答案 1 :(得分:0)

根据您的说明,我想您正在询问如何从应用的偏好中检索通知设置的值,即是应启用还是禁用通知。

您应该使用[NSUserDefaults standardUserDefaults]来访问首选项值。请参阅此Accessing Preference Values文档。