iOS UILocalNotification定時提醒

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   問題   

     在iOS中有兩類資訊提示推送方式,一類是遠程伺服器推送(APNS),還有一類就是本地通知UILocalNotification,今天就簡要的記錄一下UILocalNotification的使用,詳情如下:

 UILocalNotification *notifity=[[UILocalNotification alloc] init];        NSDateFormatter *formattr=[[NSDateFormatter alloc] init];
//格式化時間 [formattr setDateFormat:@"HH:mm"]; //觸發通知時間 NSDate *now=[formattr dateFromString:[NSString stringWithFormat:@"%@",strTimer]]; notifity.fireDate=now; //時區 notifity.timeZone=[NSTimeZone defaultTimeZone]; //通知重複提示的單位,可以是周(NSWeekdayCalendarUnit)分鐘(NSMinuteCalendarUnit)秒(NSSecondCalendarUnit)月(NSMonthCalendarUnit)年(NSYearCalendarUnit)

notifity.repeatInterval=NSDayCalendarUnit;
        //通知內容        [email protected]"這是一個通知";        //通知觸發時播放的聲音        notifity.soundName=UILocalNotificationDefaultSoundName;        //執行通知註冊        [[UIApplication sharedApplication] scheduleLocalNotification:notifity];

    如果要在通知欄中攜帶參數資訊,可以使用下面的方式:

   NSDictionary *dic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];      notification.userInfo = dic;

    如果軟體是在運行中,則可以通過AppDelegate中的回調方法擷取並處理參數資訊:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{    if (notification) {        NSDictionary *userInfo =  notification.userInfo;        NSString *obj = [userInfo objectForKey:@"key"];        NSLog(@"%@",obj);    }}

另外,可以通過兩種方式取消註冊的本地通知,一種是取消指定的通知,第二種是取消所有的註冊通知:

[[UIApplication sharedApplication] cancelLocalNotification:localNotification]; [[UIApplication sharedApplication] cancelAllLocalNotification];

以上就是我今天所學的知識,在此處記錄並且與大家分享,如有問題請指出,共同探討學習!

聯繫我們

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