ios - 用户是否可以禁用本地通知?

标签 ios ios5 notifications

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

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

我看到了这个答案: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];
}

最佳答案

如果您只有一个 localNotification 并且想取消它,您可以调用:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

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

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

如果 localNotifications.count > 0,那么您已经安排了一些通知。

关于ios - 用户是否可以禁用本地通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12102361/

相关文章:

java - 在数据库更新时调用客户端的方法

android - getIntent() Extras 总是 NULL

objective-c - 每次更改时保存列表

objective-c - 如何在 IOS5 的自动完成结果中突出显示搜索字符串?

ipad - 什么导致错误 NSNetServicesErrorCode = "-72000"?

ios - 如何将对象与通知存储在一起,以便在收到通知时可以调用它?

ios - 编写第二个排行榜

iphone - 了解使用iTunes在多个iOS设备之间进行本地核心数据同步

iPhone 针对某些设备

xcode - 为什么调试器 lldb 在 iOS 5.1 模拟器中说 object is nil ?