Ios 检查是否允许本地通知
我每天在同一时间重复一次本地通知 现在一切都很好,除了关闭通知,然后再打开,从关闭的那几天开始,通知的数量不断增加 有没有最好的方法来检查是否关闭然后再打开,并清除所有旧的通知 谢谢Ios 检查是否允许本地通知,ios,objective-c,notifications,Ios,Objective C,Notifications,我每天在同一时间重复一次本地通知 现在一切都很好,除了关闭通知,然后再打开,从关闭的那几天开始,通知的数量不断增加 有没有最好的方法来检查是否关闭然后再打开,并清除所有旧的通知 谢谢 - (void)viewDidLoad { [super viewDidLoad]; UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificat
- (void)viewDidLoad {
[super viewDidLoad];
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
// before we create any new ones, cancel all existing notifications
[[UIApplication sharedApplication]cancelAllLocalNotifications];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comp = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
comp.hour = 19; // 19 = 7PM
comp.minute = 45; // 7:45 PM
comp.second = 01; // 7:45:01 PM
localNotification.fireDate = [cal dateFromComponents:comp];
localNotification.alertBody = @"Local Notification in iOS8";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitDay;
// this will schedule the notification to fire at the fire date
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// this will fire the notification right away, it will still also fire at the date we set
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
在显示通知之前,请检查用户设置是否允许:
- (void)viewDidLoad {
[super viewDidLoad];
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
// before we create any new ones, cancel all existing notifications
[[UIApplication sharedApplication]cancelAllLocalNotifications];
if ([[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone) {
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comp = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
comp.hour = 19; // 19 = 7PM
comp.minute = 45; // 7:45 PM
comp.second = 01; // 7:45:01 PM
localNotification.fireDate = [cal dateFromComponents:comp];
localNotification.alertBody = @"Local Notification in iOS8";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitDay;
// this will schedule the notification to fire at the fire date
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// this will fire the notification right away, it will still also fire at the date we set
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
}
在打开事件切换时,请尝试以下操作-
[[UIApplication sharedApplication] cancelAllLocalNotifications];
谢谢你的回复!只需将该行代码放在[[UIApplication sharedApplication]presentLocalNotificationNow:localNotification]之前;它能起作用吗?只是检查以确保我没有遗漏任何东西。这检查以确保在您创建和安排通知之前允许通知。哦,太棒了!我得到一个错误,说uiusernotificationsettings没有可见的@interface声明选择器可能会呈现UserNotificationOfType,有什么想法吗?奇怪的是,我猜这个方法实际上是私有的,但苹果无意中将其包含在他们的文档中。我用另一种方法更新了代码,这很奇怪。谢谢,我要试试这个!