gpt4 book ai didi

ios - 检查是否允许本地通知

转载 作者:行者123 更新时间:2023-11-28 17:51:55 25 4
gpt4 key购买 nike

我每天在同一时间重复本地通知。

现在一切正常,除了当通知被关闭然后重新打开时,从它关闭的那一天起,通知就会累积起来。

有没有最好的方法来检查是否关闭然后重新打开,并清除所有旧通知?

谢谢!

- (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];

}
}

关于ios - 检查是否允许本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30550875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com