I have my app with data coming from server. I have 2 type of notifications say monthly.Now i will get data say for month with (date-time format) 2-2:30,6-9:00,23-10:00,26-12:00. where 2 denotes date followed by time with colon (:) format.
我的应用程序包含来自服务器的数据。我有2种类型的通知说每月。现在我将获得数据说月份(日期时间格式)2-2:30,6-9:00,23-10:00,26-12:00。其中2表示日期,后跟冒号(:)格式的时间。
Now I want to run the notification every month on the same dates and time. I am able to run the multiple notifications. But they do not repeat every month. How to do that. Following is my code for the same. Please help what I am missing.
现在我想每个月在相同的日期和时间运行通知。我能够运行多个通知。但他们不会每个月都重复。怎么做。以下是我的相同代码。请帮助我失踪。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[UIApplication sharedApplication].idleTimerDisabled = YES;
UIUserNotificationSettings *notiSett = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:NULL];
[[UIApplication sharedApplication]registerUserNotificationSettings:notiSett];
[self generateLocalNotificationDaily];
}
-(void)generateLocalNotificationDaily
{
// [[UIApplication sharedApplication] cancelAllLocalNotifications];
[self compareDateDaily];
}
-(void)compareDateDaily
{
NSString *strTime = [Helper getPREF:PREF_AlARM_DAILY_TIME];
NSArray *arrtime = [strTime componentsSeparatedByString:@","];
for(int i=0;i<[arrtime count];i++)
{
NSArray *arrfirstTime = [[NSString stringWithFormat:@"%@",[arrtime objectAtIndex:i]]componentsSeparatedByString:@":"];
int hh = (int)[[arrfirstTime objectAtIndex:0]integerValue];
int mm = (int)[[arrfirstTime objectAtIndex:1]integerValue];
NSDate *now = [NSDate date];
NSTimeInterval secondsPerDay = 1;//24 * 60 * 60;
NSDate *date = [now dateByAddingTimeInterval:secondsPerDay];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
[components setHour:hh];
[components setMinute:mm];
NSDate *todaySpecificDate = [calendar dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *currentDate = [NSDate date];
NSString *strCurrentDate = [formatter stringFromDate:currentDate];
NSString *strSystemDate = [formatter stringFromDate:todaySpecificDate];
currentDate = [formatter dateFromString:strCurrentDate];
todaySpecificDate = [formatter dateFromString:strSystemDate];
NSComparisonResult result = [currentDate compare:todaySpecificDate];
NSString *strAlertMessage = [NSString stringWithFormat:@"%@",[Helper getPREF:PREF_AlARM_MESSAGE]];
NSString *strUserName = [Helper getPREF:PREF_NAME];
NSString *strmsg = [strAlertMessage stringByReplacingOccurrencesOfString:@"user_name" withString:strUserName];
NSString *alertMsg = [strmsg stringByReplacingOccurrencesOfString:@"notification_type" withString:@"daily"];
//
switch (result)
{
case NSOrderedAscending:
{
UILocalNotification* localNotificationDaily = [[UILocalNotification alloc] init];
localNotificationDaily.fireDate = todaySpecificDate;
localNotificationDaily.alertBody = alertMsg;
localNotificationDaily.repeatInterval = NSCalendarUnitDay;
localNotificationDaily.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotificationDaily];
NSLog(@"%@ is in future from %@", todaySpecificDate, currentDate);
}
break;
case NSOrderedDescending:
NSLog(@"%@ is in past from %@", todaySpecificDate, currentDate);
break;
case NSOrderedSame:
NSLog(@"%@ is the same as %@", todaySpecificDate, currentDate);
break;
default:
NSLog(@"erorr dates %@, %@", todaySpecificDate, currentDate);
break;
}
}
}
1 个解决方案
#1
2
there is a property in uiLocalNotification "repeatInterval". You have to set the repeat interval to month like this
uiLocalNotification中有一个属性“repeatInterval”。你必须像这样将重复间隔设置为月份
localNotificationMonthly.repeatInterval = NSCalendarUnitMonth;
It will repeat your notification every month depending upon the date and time you set for the notification. For further inquiry please refer to
它将每月重复您的通知,具体取决于您为通知设置的日期和时间。如需进一步咨询,请参阅
https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/repeatInterval
for week repeat interval would be NSCalendarUnitWeekOfYear
星期重复间隔将是NSCalendarUnitWeekOfYear
#1
2
there is a property in uiLocalNotification "repeatInterval". You have to set the repeat interval to month like this
uiLocalNotification中有一个属性“repeatInterval”。你必须像这样将重复间隔设置为月份
localNotificationMonthly.repeatInterval = NSCalendarUnitMonth;
It will repeat your notification every month depending upon the date and time you set for the notification. For further inquiry please refer to
它将每月重复您的通知,具体取决于您为通知设置的日期和时间。如需进一步咨询,请参阅
https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/repeatInterval
for week repeat interval would be NSCalendarUnitWeekOfYear
星期重复间隔将是NSCalendarUnitWeekOfYear