需求:
离线的24小时、72小时、168小时,向用户发出召回推送。
如果用户退出APP的时间是12:00之前就在第二天、第四天的12:00、第八天的18:00发推送。
如果用户退出APP的时间是12:00之前就在第三天、第五天的12:00、第九天的18:00发推送。
实现:
思路:在用户将APP退到后台的时候添加本地推送。
- (void)applicationDidEnterBackground:(UIApplication *)application {
//清除所有通知(包含本地通知)
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self addLocalNotification];
}
-(void)addLocalNotification{
//获取当前时间
NSDate *date = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *currentDate = [date dateByAddingTimeInterval: interval];
NSDate *tempDate = [self getCustomDateWithHour:12];
NSDate *targetDate = [tempDate dateByAddingTimeInterval: interval];
//获得nowDate和selfDate的差距
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitSecond fromDate:currentDate toDate:targetDate options:0];
[self addLocalNotificationWithTimeInterval:(cmps.second > 0) ? (24*60*60+cmps.second):(48*60*60+cmps.second) withType:1];
[self addLocalNotificationWithTimeInterval:(cmps.second > 0) ? (72*60*60+cmps.second) : (96*60*60+cmps.second) withType:2];
[self addLocalNotificationWithTimeInterval:(cmps.second > 3*60*60) ? (168*60*60+cmps.second+3*60*60) : (192*60*60+cmps.second+3*60*60) withType:3];
}
/**
* @brief 生成当天的某个点(返回的是伦敦时间,可直接与当前时间[NSDate date]比较)
* @param hour 如hour为“8”,就是上午8:00(本地时间)
*/
- (NSDate *)getCustomDateWithHour:(NSInteger)hour
{
//获取当前时间
NSDate *currentDate = [NSDate date];
NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *currentComps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
//设置当天的某个点
NSDateComponents *resultComps = [[NSDateComponents alloc] init];
[resultComps setYear:[currentComps year]];
[resultComps setMonth:[currentComps month]];
[resultComps setDay:[currentComps day]];
[resultComps setHour:hour];
NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
return [resultCalendar dateFromComponents:resultComps];
}
- (void)addLocalNotificationWithTimeInterval:(NSTimeInterval )timeInterval withType:(NSInteger )type{
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil && (timeInterval > 0)) {//判断系统是否支持本地通知
NSDate *fireDate = [NSDate date];
notification.fireDate = [fireDate dateByAddingTimeInterval:timeInterval];
// notification.fireDate = [NSDate dateWithTimeIntervalSince1970:10];//本次开启立即执行的周期 since1970并非是在1970年1月1日的早上零点开始算的,而是根据安装该软件的手机的系统时区所对应的时区的得出来的时间,换算成北京时间,这个since1970所得出的起始时间就是1970年1月1日早上8点
// notification.repeatInterval=kCFCalendarUnitMinute;//循环通知的周期
notification.timeZone=[NSTimeZone defaultTimeZone];
NSArray *localNotificationArray;
if (type == 1) {
localNotificationArray = @[@"今天去哪玩啦?",@"我等你回来哦~",@"难道你还要错过吗?快回来!"];
}
else if (type == 2){
localNotificationArray = @[@"三日不见,如隔九秋!",@"请不要忘记我!",@"我你已经三天没来啦"];
}
else{
localNotificationArray = @[@"讲",@"快快回归",@"七日不见"];
}
int k= rand()%(localNotificationArray.count);
notification.alertBody=localNotificationArray[k];//弹出的提示信息
notification.applicationIconBadgeNumber=0; //应用程序的右上角小数字
notification.soundName= UILocalNotificationDefaultSoundName;//本地化通知的声音
notification.hasAction = NO;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}