iOS開發本地通知(每天定點發送訊息推送)

來源:互聯網
上載者:User

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];
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.