標籤: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];
以上就是我今天所學的知識,在此處記錄並且與大家分享,如有問題請指出,共同探討學習!