如何在iOS(ObjC)中安排每日本地推送通知? [英] How to schedule daily local push notification in iOS (ObjC)?
问题描述
不能以正确的方式安排每日本地PushNotification
.
我只想在上午9:00每天只显示一个本地PushNotification,其中包含今天的计数任务.
Can't schedule daily local PushNotification
in correct way.
I want to show only one daily local PushNotification at 9:00 AM with counted tasks for today.
我的代码仅在didFinishLaunchingWithOptions
中执行一次,例如:
My code executed only once in 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
值随机触发它,并在随机时间重复几次.
Looks like I miss something, because it's really triggered at 9:00, but with wrong data in _todayTasks
, also it can be randomly triggered with other _todayTasks
values and repeated few times at random time.
推荐答案
分步操作:
-
注册通知:
Register notifications:
- (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点),则创建通知:
Create notification if user allow local Notifications (example 3pm every day):
- (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
通知
Also don't forget to call [[UIApplication sharedApplication] cancelAllLocalNotifications];
- this will clean up all notification and u will remove all random
notifications
这篇关于如何在iOS(ObjC)中安排每日本地推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!