2

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

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

我看到了这个答案:在 iPhone 上确定用户是否启用了推送通知 ,但我不知道将其放入何处并正确编程(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];
}
4

2 回答 2

1

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

[[UIApplication sharedApplication] cancelAllLocalNotifications];

如果没有任何 localNotifications,这不会引发错误。您可以通过按 IBAction 的按钮来调用它。如果您想根据是否有任何 localNotifications 设置开关的状态,那么您可以检查它是否有:

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

如果 localNotifications.count > 0 则您安排了一些通知。

于 2012-08-24T22:46:51.490 回答
0

根据您的描述,我认为您在问如何从您的应用程序的偏好中检索通知设置的值,即是否应该启用或禁用通知。

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

于 2012-08-26T05:21:26.337 回答