iOS开发本地通知(每天3点半发送消息推送)

NSDate *now = [NSDate date];
//取得系统时间
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
components = [calendar components:unitFlags fromDate:now];
NSInteger hour = [components hour];
NSInteger min = [components minute];
NSInteger sec = [components second];
NSInteger week = [components weekday];
NSLog(@"现在是%ld:%ld:%ld,周%ld",hour,min,sec,week);

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//设置时区(跟随手机的时区)
localNotification.timeZone = [NSTimeZone defaultTimeZone];
if (localNotification) {
    localNotification.alertBody = @"消息推送消息推送";
    localNotification.alertAction = @"打开";
    //小图标数字
    localNotification.applicationIconBadgeNumber = 0;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSDate *date = [formatter dateFromString:@"15:30:00"];
    //通知发出的时间
    localNotification.fireDate = date;
}
//循环通知的周期
localNotification.repeatInterval = kCFCalendarUnitDay;

//设置userinfo方便撤销
NSDictionary *info = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
localNotification.userInfo = info;
//启动任务
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
 */
/*
for (UILocalNotification *obj in [UIApplication sharedApplication].scheduledLocalNotifications) {
    if ([obj.userInfo.allKeys containsObject:kLocalNotificationKey]) {
        [[UIApplication sharedApplication] cancelLocalNotification:obj];
    }
}




// 设置一个按照固定时间的本地推送
NSDate *now = [NSDate date];
//取得系统时间
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
components = [calendar components:unitFlags fromDate:now];
NSInteger hour = [components hour];
NSInteger min = [components minute];
NSInteger sec = [components second];
NSInteger week = [components weekday];
WDLog(@"现在是%ld:%ld:%ld,周%ld",hour,min,sec,week);

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//设置时区(跟随手机的时区)
localNotification.timeZone = [NSTimeZone defaultTimeZone];
if (localNotification) {
    localNotification.alertBody = @"消息推送消息推送";
    localNotification.alertAction = @"打开";
    //小图标数字
    localNotification.applicationIconBadgeNumber = 0;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSDate *date = [formatter dateFromString:@"15:30:00"];
    //通知发出的时间
    localNotification.fireDate = date;
}
//循环通知的周期
localNotification.repeatInterval = kCFCalendarUnitDay;

//设置userinfo方便撤销
NSDictionary *info = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
localNotification.userInfo = info;
//启动任务
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
 */
/*
for (UILocalNotification *obj in [UIApplication sharedApplication].scheduledLocalNotifications) {
    if ([obj.userInfo.allKeys containsObject:kLocalNotificationKey]) {
        [[UIApplication sharedApplication] cancelLocalNotification:obj];
    }
}
//直接取消全部本地通知
//[[UIApplication sharedApplication] cancelAllLocalNotifications];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容