【问题标题】:Is the user able to disable local notifications?用户是否能够禁用本地通知?
【发布时间】:2012-08-19 14:27:29
【问题描述】:

我创建了一个应用程序,该应用程序将在每天上午 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];
}

【问题讨论】:

  • 经过进一步研究,我发现了这个,我认为这是我需要做的:useyourloaf.com/blog/2010/05/18/… 但是,我不确定在此代码中要更改什么:- (BOOL)shouldAutorotateToInterfaceOrientation:( UIInterfaceOrientation)interfaceOrientation { // 获取用户偏好 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; BOOL 启用 = [默认 boolForKey:@"enableRotation"];如果(启用){返回是; } else { return (interfaceOrientation == UIInterfaceOrientationPortrait); } }

标签: ios ios5 notifications


【解决方案1】:

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

[[UIApplication sharedApplication] cancelAllLocalNotifications];

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

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

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

【讨论】:

  • 嗨 SkinnyTOD 我将如何将其实现为 IF/THEN 语句?
  • @user1114118:我不明白你的问题。
  • 对不起。我的编码知识非常有限。我的理解是我需要它:1)检查用户是否在设置包中打开或关闭了本地通知 2)如果他们打开了,请设置通知。 3)如果他们不这样做,什么也不做。我想我不知道我需要做什么才能在代码中实现它。
  • 首先从文档开始:developer.apple.com/library/ios/#DOCUMENTATION/… - 编程本身并不难,而且有很多教程。挑战在于细节:您的应用程序可以处于的各种状态(活动、未运行、通知关闭等)以及您如何处理每种状态的通知。我建议制作一个测试项目来关注这一点。匆匆忙忙,复制不懂的代码会浪费精力。
【解决方案2】:

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

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

【讨论】:

  • 谢谢你们!我可以这样设置: if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableNotifications"]) { CODE TO ENABLE NOTIFICATIONS } else {[[UIApplication sharedApplication] cancelAllLocalNotifications];} return [super getCommandInstance:className]; }
猜你喜欢
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 2012-01-28
  • 2013-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多