无法安排每日本地PushNotification
以正确的方式。
我只想在上午 9:00 显示一个每日本地 PushNotification,其中包含今天的计数任务。
我的代码只执行一次didFinishLaunchingWithOptions
like:
- (void)scheduleLocalNotification
{
self.localNotification = [UILocalNotification new];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];
components.hour = 9;
components.minute = 0;
components.second = 0;
NSDate *fireTime = [calendar dateFromComponents:components];
_localNotification.fireDate = fireTime;
_localNotification.alertBody = [NSString stringWithFormat:@"Hi, <username>. You have %ld tasks for today", (long)_todayTasks];
_localNotification.repeatInterval = NSCalendarUnitDay;
_localNotification.soundName = @"alarm.wav";
_localNotification.timeZone = [NSTimeZone localTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:_localNotification];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
NotificationManagerLog(@"schedule local notification at %@", [dateFormatter stringFromDate:fireTime]);
}
看起来我错过了一些东西,因为它确实是在 9:00 触发的,但是数据错误_todayTasks
,也可以与其他随机触发_todayTasks
值并随机重复几次。
一步步:
-
注册通知:
- (void)prepareLocalNotification
{
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
}
-
如果用户允许本地通知,则创建通知(例如每天下午 3 点):
- (void)createDailyBasisNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *next3pm = [self notificationForTime:15 withMessage:<NotificationMessage3PM> soundName:UILocalNotificationDefaultSoundName];
[[UIApplication sharedApplication] scheduleLocalNotification:next3pm];
}
- (UILocalNotification *)notificationForTime:(NSInteger)time withMessage:(NSString *)message soundName:(NSString *)soundName
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = message;
localNotification.repeatInterval = NSDayCalendarUnit;
localNotification.timeZone = [NSTimeZone localTimeZone];
localNotification.soundName = soundName;
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date];
[dateComponents setHour:time];
NSDate *next = [calendar dateFromComponents:dateComponents];
if ([next timeIntervalSinceNow] < 0) {
next = [next dateByAddingTimeInterval:60*60*24];
}
localNotification.fireDate = next;
return localNotification;
}
也不要忘记打电话[[UIApplication sharedApplication] cancelAllLocalNotifications];
- 这将清除所有通知,您将删除所有通知random
通知
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)