iOS開發中UILocalNotification實現本地通知實現提醒功能

來源:互聯網
上載者:User

標籤:

蘋果手機開發中的資訊提示推送方式,一類是遠程伺服器推送(APNS)與UILocalNotification本地通知的,下面我來介紹第二種的使用方法。 這兩天在做一個鬧鐘提醒功能,用到了本地通知的功能,記錄相關知識如下:

1、本地通知的定義和使用:

本地通知是UILocalNotification的執行個體,主要有三類屬性:

scheduled time,時間周期,用來指定iOS系統發送通知的時間和日期;

notification type,通知類型,包括警告資訊、動作按鈕的標題、應用表徵圖上的badge(數字標記)和播放的聲音;

自訂資料,本地通知可以包含一個dictionary類型的本機資料。

對本地通知的數量限制,iOS最多允許最近本地通知數量是64個,超過限制的本地通知將被iOS忽略。

    UILocalNotification *notification=[[UILocalNotification alloc] init];    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10];    notification.alertBody=@"鬧鐘響了。";    notification.alertTitle=@"請開啟鬧鐘";    notification.repeatInterval=NSCalendarUnitSecond;    notification.applicationIconBadgeNumber=1;        notification.userInfo[email protected]{@"name":@"zhangsanfeng"};    notification.soundName=UILocalNotificationDefaultSoundName;    //[[UIApplication sharedApplication]scheduleLocalNotification:notification];    //[[UIApplication sharedApplication]presentLocalNotificationNow:notification];

2、取消本地通知:

        UIApplication *app=[UIApplication sharedApplication];        NSArray *array=[app scheduledLocalNotifications];        NSLog(@"%ld",array.count);                for (UILocalNotification * local in array) {            NSDictionary *dic= local.userInfo;            if ([dic[@"name"] isEqual:@"zhangsanfeng"]) {                //刪除指定的通知                [app cancelLocalNotification:local];            }        }        //也可以使用[app cancelAllLocalNotifications]刪除所有通知;    

3、本地通知的響應:

如果已經註冊了本地通知,當用戶端響應通知時:

    a、應用程式在背景時候,本地通知會給裝置送達一個和遠程通知一樣的提醒,提醒的樣式由使用者在手機設定中設定

    b、應用程式正在運行中,則裝置不會(而是應用程式)收到提醒,但是會走應用程式delegate中的方法:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{    //如果你想實現程式在後台時候的那種提醒效果,可以在這個方法中添加相關代碼(添加個警告視圖)}

需要注意的是,在情況a中,如果使用者點擊提醒進入應用程式,也會執行收到本地通知的回調方法,這種情況下如果你添加了上面那段代碼,則會出現連續出現兩次提示,為瞭解決這個問題,修改代碼如下:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{    //判斷應用程式當前的運行狀態,如果是啟用狀態,則進行提醒,否則不提醒    if (application.applicationState == UIApplicationStateActive) {        UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"計時結束"                                                                    message:@"您的鬧鐘已到"                                                             preferredStyle:UIAlertControllerStyleAlert];        UIAlertAction*action1=[UIAlertAction actionWithTitle:@"嗯我知道了"                                                       style:UIAlertActionStyleDefault                                                     handler:^(UIAlertAction*action){                                                         // [self doSomeThing];                                                     }];        UIAlertAction*action2=[UIAlertAction actionWithTitle:@"別吵了我日"                                                       style:UIAlertActionStyleDestructive //警告樣式,紅色                                                     handler:^(UIAlertAction*action){                                                        // [self doSomeThing];                                                     }];                [alert addAction:action2];        [alert addAction:action1];                [self presentViewController:alert animated:YES completion:nil];    }}

4、需要注意:What is the difference between presentLocalNotificationNow and scheduleLocalNotification?

There‘s no difference right here, but using scheduleLocalNotification you can schedule it at whatever time you want, not only in one second. But presentLocalNotificationNow will show one in exactly one second, not in 0.5 or 2.0 in iOS 8, for example.

前提是你的應用程式不處於啟用狀態,本地通知才會有效果;

iOS開發中UILocalNotification實現本地通知實現提醒功能

聯繫我們

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