本地通知UILocalNotification

在應(yīng)用中有的時候需要做一個類似于鬧鐘提醒的功能,在每周的特定的幾天中,在固定時間進行提醒。比如每周一到周五下午六點,需要吃藥的時間就彈窗提醒:快去吃藥!不要放棄治療!
本地固定時間提醒,需要用到 UILocalNotification
如果只是添加一次,并且設(shè)置提醒的周期為NSCalendarUnitWeekOfYear ,那么就是每周只提醒一次。所以如果要達到每周固定星期幾提醒則需要添加提醒天的次數(shù),比如周一到周五提醒那么就需要添加五次,每個提醒的間隔都為一個星期。這樣就可以做到每周固定星期提醒了。

for(id day in _weekArr) {// _weekArr 為周一到周日需要提醒的星期 
    UILocalNotification* beginLocalNotification = [[UILocalNotification alloc] init]; 
    NSDictionary* beginInfo = [NSDictionary dictionaryWithObject:kLocalNoti forKey:@"beginNoti"];
    beginLocalNotification.userInfo = beginInfo;
    beginLocalNotification.fireDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"2015-06-%d %@:00",15+[day integerValue]-1,_beginTime]];// 在過去的某個星期內(nèi)添加需要的本地提醒。并且設(shè)置提醒間隔為NSCalendarUnitWeekOfYear,如果添加在未來,那么提醒不會發(fā)生。 
    beginLocalNotification.alertBody = @"該吃藥啦!";
    beginLocalNotification.soundName = @"ping.caf"; 
    beginLocalNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    beginLocalNotification.alertAction = @"不要忘記吃藥哦!"; 
    beginLocalNotification.repeatInterval = NSCalendarUnitWeekOfYear;// 每周循環(huán)提醒
    //按照規(guī)定的時候觸發(fā)
    [[UIApplication sharedApplication] scheduleLocalNotification:beginLocalNotification];
    //立即觸發(fā)一個通知
    //[[UIApplication sharedApplication] presentLocalNotificationNow: beginLocalNotification]; 
}

取消提醒比較簡單,如果想取消所有的本地提醒:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

取消特定的提醒:

NSArray *narry=[[UIApplication sharedApplication] scheduledLocalNotifications]; 
NSUInteger acount=[narry count]; 
if (acount<1) { 
    return false; 
} 
for (int i=0; i<acount; i++) { 
    UILocalNotification *myUILocalNotification = [narry objectAtIndex:i]; 
    NSDictionary *userInfo = myUILocalNotification.userInfo; 
    NSString *obj = [userInfo objectForKey:@"beginNoti"]; 
    if ([obj isEqualToString @"kLocalNoti "]) { 
        [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification]; 
        return true; 
    } 
}

點擊提醒
App在前臺或者在后臺時,即運行時接收到本地推送消息。上面的2種情況的處理基本一致, 不同點只有當(dāng)運行再后臺的時候,會有彈窗提示用戶另外一個App有通知,對于本地通知單的處理都是通過*AppDelegate的方法:- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification來處理的。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"接收到本地提醒 in app" message:notification.alertBody delegate:nil  cancelButtonTitle:@"確定" otherButtonTitles:nil];
    [alert show];
    //這里,你就可以通過notification的useinfo,干一些你想做的事情了
    application.applicationIconBadgeNumber -= 1;
}

App沒有啟動,點擊通知。這種情況下,當(dāng)點擊通知時,會啟動App,而在App中,開發(fā)人員可以通過實現(xiàn)*AppDelegate中的方法:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions,然后從lauchOptions中獲取App啟動的原因,若是因為本地通知,則可以App啟動時對App做對應(yīng)的操作。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
{  
    NSLog(@"Start App....");  
    ....  
    UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];  
    if ([self isWelcomeNotification:localNotification]) {  
        NSLog(@"start with welcome notification");  
        [self.mainViewController showOfficeIntroduction];  
    }  
    return YES;  
}  

repeatInterval的循環(huán)枚舉


typedef NS_OPTIONS(NSUInteger, NSCalendarUnit) {
        NSCalendarUnitEra                = kCFCalendarUnitEra,//一個年代、一個世紀(jì)循環(huán)一次。
        NSCalendarUnitYear               = kCFCalendarUnitYear,//每年循環(huán)一次
        NSCalendarUnitMonth              = kCFCalendarUnitMonth,//每月循環(huán)一次
        NSCalendarUnitDay                = kCFCalendarUnitDay,//每天循環(huán)一次
        NSCalendarUnitHour               = kCFCalendarUnitHour,//每小時循環(huán)一次
        NSCalendarUnitMinute             = kCFCalendarUnitMinute,//每分鐘循環(huán)一次
        NSCalendarUnitSecond             = kCFCalendarUnitSecond,//每秒循環(huán)一次
        NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,//平常日、工作日循環(huán),但是周末也會。(*而不是每周循環(huán),具體和NSCalendarUnitDay有什么區(qū)別,暫時沒有測試出來,如果哪位知道請告知,感謝)
        NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,
        NSCalendarUnitQuarter            NS_ENUM_AVAILABLE(10_6, 4_0) = kCFCalendarUnitQuarter,//一個季度循環(huán)一次
        NSCalendarUnitWeekOfMonth        NS_ENUM_AVAILABLE(10_7, 5_0) = kCFCalendarUnitWeekOfMonth,//每個月的第幾周
        NSCalendarUnitWeekOfYear         NS_ENUM_AVAILABLE(10_7, 5_0) = kCFCalendarUnitWeekOfYear,//每年的第幾周
        NSCalendarUnitYearForWeekOfYear  NS_ENUM_AVAILABLE(10_7, 5_0) = kCFCalendarUnitYearForWeekOfYear,
        NSCalendarUnitNanosecond         NS_ENUM_AVAILABLE(10_7, 5_0) = (1 << 15),
        NSCalendarUnitCalendar           NS_ENUM_AVAILABLE(10_7, 4_0) = (1 << 20),
        NSCalendarUnitTimeZone           NS_ENUM_AVAILABLE(10_7, 4_0) = (1 << 21),
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容