用户能够禁用本地通知吗?

11 浏览
0 Comments

用户能够禁用本地通知吗?

我创建了一个应用程序,可以在每天早上9点给用户发送通知,但希望用户可以选择关闭它。

我为应用程序设置了一个设置包,并设置了一个用于启用通知的滑块。当我构建和测试应用程序时,设置是存在的,我可以打开或关闭它们...但似乎应用程序没有注册它。我应该如何编程应用程序来检查通知设置是否打开或关闭?

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

0
0 Comments

用户是否能够禁用本地通知是一个常见的问题,用户可能希望在某些情况下禁用应用程序的通知功能。下面将从原因和解决方法两个方面来整理这个问题。

问题的出现原因:

该问题的出现原因是用户想要从应用程序的首选项中检索通知设置的值,即通知是否启用或禁用。

解决方法:

解决方法是使用`[NSUserDefaults standardUserDefaults]`来访问首选项的值。

具体代码如下:

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableNotifications"]) {
    // 启用通知的代码
} else {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

以上代码将检查名为"enableNotifications"的首选项值,如果该值为`true`,则启用通知功能;如果该值为`false`,则取消所有本地通知。

以上就是关于用户是否能够禁用本地通知问题的原因和解决方法的整理。

0
0 Comments

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

在实现这个功能之前,我们首先需要了解用户是否已经开启了本地通知功能。我们可以通过检查设置中的开关状态来判断用户是否开启了本地通知。如果用户开启了本地通知,我们可以设置通知;如果用户没有开启本地通知,则不进行任何操作。

要实现这个功能,我们可以通过以下步骤来完成:

1. 首先,我们需要引用UIApplication类,以便可以使用它的实例来取消所有本地通知。

[[UIApplication sharedApplication] cancelAllLocalNotifications];

这段代码将取消所有的本地通知。如果没有任何本地通知,这段代码也不会报错。

2. 接下来,我们需要检查是否有本地通知被设置。我们可以使用下面的代码来获取已经设置的本地通知数组:

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

如果localNotifications.count > 0,说明已经设置了一些通知。

3. 如果要根据是否有本地通知设置开关的状态,我们可以使用以下代码来实现:

if (localNotifications.count > 0) {
    // 设置开关为开启状态
    // ...
} else {
    // 设置开关为关闭状态
    // ...
}

通过以上步骤,我们可以根据用户是否开启本地通知来设置开关的状态。如果用户开启了本地通知,我们可以根据需要设置通知;如果用户没有开启本地通知,则不进行任何操作。

需要注意的是,开发本地通知功能还涉及到应用程序的各种状态(激活状态、未运行状态、通知关闭等)以及如何处理每种状态下的通知。建议在此之前创建一个测试项目,重点研究这些细节。匆忙复制不理解的代码只会导致浪费精力。

参考文档:[developer.apple.com/library/ios/#DOCUMENTATION/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008194](developer.apple.com/library/ios/#DOCUMENTATION/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008194)

0