iOS建立本地通知和刪除對應的通知,工作日通知

來源:互聯網
上載者:User

標籤:

本文的代碼主要是:建立本地通知,刪除對應的本地通知,建立工作日鬧鐘

直接上代碼:

////  ViewController.m//  LocalNSNotification////  Created by wusiping on 16/1/27.//  Copyright © 2016年 wusiping. All rights reserved.//#import "ViewController.h"#define LOCAL_NOTIFY_SCHEDULE_ID @"hahahhahahhah"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        UIButton *createBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];    [createBtn setTitle:@"建立通知" forState:UIControlStateNormal];    createBtn.titleLabel.textColor = [UIColor blackColor];    createBtn.backgroundColor = [UIColor lightGrayColor];    [createBtn addTarget:self action:@selector(createNSNotification) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:createBtn];        UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];    [cancelBtn setTitle:@"刪除通知" forState:UIControlStateNormal];    cancelBtn.titleLabel.textColor = [UIColor blackColor];    cancelBtn.backgroundColor = [UIColor lightGrayColor];    [cancelBtn addTarget:self action:@selector(cancelNSNotification) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:cancelBtn];}- (NSDate *)getCurrrentTimeToSS{    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    [formatter dateFormat];    [NSDate date];    return [NSDate date];    }- (void)createNSNotification{        UILocalNotification *localNotification = [[UILocalNotification alloc] init];    if (localNotification == nil) {        return;    }    //設定本地通知的觸發時間(如果要立即觸發,無需設定),這裡設定為20妙後    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:20];    //設定本地通知的時區    localNotification.timeZone = [NSTimeZone defaultTimeZone];    //設定通知的內容    localNotification.alertBody = @"hahhah";    //設定通知動作按鈕的標題    localNotification.alertAction = @"查看";    //設定提醒的聲音,可以自己添加音效檔,這裡設定為預設提示聲    localNotification.soundName = UILocalNotificationDefaultSoundName;    //設定通知的相關資訊,這個很重要,可以添加一些標記性內容,方便以後區分和擷取通知的資訊    NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:9529],@"nfSignInkey",nil];    [localNotification setUserInfo:dict];    // ios8後,需要添加這個註冊,才能得到授權    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type                                                                                 categories:nil];        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];        // 通知重複提示的單位,可以是天、周、月        localNotification.repeatInterval = NSCalendarUnitDay;    } else {        // 通知重複提示的單位,可以是天、周、月        localNotification.repeatInterval = NSDayCalendarUnit;    }    //在規定的日期觸發通知    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];}/** *  刪除當前的通知 */- (void)cancelNSNotification{        //   手動刪除通知    //   這裡我們要根據我們添加時設定的key和自訂的ID來刪        NSArray *narry=[[UIApplication sharedApplication] scheduledLocalNotifications];        NSUInteger acount=[narry count];        if (acount>0)        {            // 遍曆找到對應nfkey和notificationtag的通知            for (int i=0; i<acount; i++)            {                UILocalNotification *myUILocalNotification = [narry objectAtIndex:i];                NSDictionary *userInfo = myUILocalNotification.userInfo;                NSNumber *obj = [userInfo objectForKey:@"nfSignInkey"];                int mytag=[obj intValue];                if (mytag==9529)                {                    // 刪除本地通知                    [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];                    break;                }            }        }}@end

大家可能會遇到工作日鬧鐘這種需求:
 if ([cycle isEqual:@"工作日"]) {//如果是工作日鬧鐘            NSDate *now=select;//目前時間            NSCalendar *gregorian = [NSCalendar currentCalendar];            NSDateComponents *dateComps = [gregorian components:NSWeekdayCalendarUnit fromDate:now];            NSInteger daycount = [dateComps weekday]-2;            NSDate *weekdaybegin=[now addTimeInterval:-daycount*60*60*24];            for (int i=0;i<5;i++) {//建立5個通知:星期一到星期五                NSDate *now= [weekdaybegin addTimeInterval:i*60*60*24];                [self creatmessage:now];            }   }-(void)creatmessage:(NSDate *)new{    UILocalNotification *notification=[[UILocalNotification alloc] init];    if (notification!=nil && select!=nil &&cycle.length > 0 &&key.intValue==1)    {        //觸發通知時間        //[self cancelUILocalNotification];        NSDate *now= new;        NSLog(@"%@",now);        NSDate *today = [self getCurrrentTimeToDay];        BOOL isSameDay = [self isSameDay:now date2:today];        if (isSameDay == YES){//特殊需求..這個不要管            //今天所在的星期X,延遲一個禮拜觸發。比如今天是星期三,那下個禮拜三才開始鬧鐘。明天是星期四,星期四的鬧鐘還是明天就觸發            now = [NSDate dateWithTimeInterval:24*60*60*7 sinceDate:now];                    }        notification.fireDate=now;        notification.timeZone=[NSTimeZone defaultTimeZone];        notification.repeatInterval = kCFCalendarUnitWeek;        notification.soundName=@"鈴聲.m4r";        notification.alertBody=@"今天iSite簽到沒?千萬不要忘了哦!";        notification.alertAction=NSLocalizedString(@"今天iSite簽到沒?千萬不要忘了哦!", nil);        NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:9529],@"nfSignInkey",nil];        [notification setUserInfo:dict];                // 啟用這個通知        [[UIApplication sharedApplication]   scheduleLocalNotification:notification];    }    }

 

 

 

 

iOS建立本地通知和刪除對應的通知,工作日通知

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.