輸出!發送訊息至通知列
UILocalNotification是一個特別的通知物件,它可以使你的應用程序可以在特定的日期和時間發送通知,下面要完成2個類型,第一個為立即發送訊息,另一個為15秒後再發送訊息,範例中的Button使用程式方式建立,來讓整個設定過程更詳細,這方面熟練一點對於往後要自制UI元件時更得心應手,不過你還是可以使用storyboard建立Button來省略範例程式中的一些程式碼。
執行結果
UI 畫面
第一次執行會詢問是否授權該APP使用通知
)
發送後通知列上面的結果
Swift 程式範例
//-----------start----------- // // ViewController.swift // // import UIKit class ViewController: UIViewController { var dyNotificationNowButton: UIButton! var dyNotificationFireButton: UIButton! override func viewDidLoad() { super.viewDidLoad() // 註冊通知使用聲音、訊息文字,通知無分類 UIApplication.sharedApplication().registerUserNotificationSettings( UIUserNotificationSettings( forTypes:UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil) ) // Notification Now Button // 寬度100 高度30 按鍵 dyNotificationNowButton = UIButton(frame: CGRectMake(0,0,250,60)) dyNotificationNowButton.backgroundColor = UIColor.grayColor() // 按下時文字變黑色 dyNotificationNowButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted); dyNotificationNowButton.layer.masksToBounds = true // Button 設定文字內容 dyNotificationNowButton.setTitle("Notification Now", forState: .Normal) // Button圓角10 dyNotificationNowButton.layer.cornerRadius = 10.0 // Button位於X座標中心及Y座標200 dyNotificationNowButton.layer.position = CGPoint(x: self.view.bounds.width/2, y:200) // 按下後執行onClickButton dyNotificationNowButton.addTarget(self, action: "onClickButton:", forControlEvents: .TouchUpInside) // Button Tag =2 dyNotificationNowButton.tag = 1 // Notification Fire Button // 寬度100 高度30 按鍵 dyNotificationFireButton = UIButton(frame: CGRectMake(0,0,250,60)) dyNotificationFireButton.backgroundColor = UIColor.greenColor() // 按下時文字變黑色 dyNotificationFireButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted); dyNotificationFireButton.layer.masksToBounds = true // Button 設定文字內容 dyNotificationFireButton.setTitle("Notification FireDate", forState: .Normal) // Button圓角10 dyNotificationFireButton.layer.cornerRadius = 10.0 // Button位於X座標中心及Y座標300 dyNotificationFireButton.layer.position = CGPoint(x: self.view.bounds.width/2, y:300) // 按下後執行onClickButton dyNotificationFireButton.addTarget(self, action: "onClickButton:", forControlEvents: .TouchUpInside) // Button Tag =2 dyNotificationFireButton.tag = 2 // 加入兩個功能Button view.addSubview( dyNotificationNowButton) view.addSubview( dyNotificationFireButton) } /* Button處理事件,這邊使用Tag判斷按下的是哪個按鍵 */ func onClickButton(sender: UIButton){ if sender.tag == 1 { showNotificationNow() } else if sender.tag == 2 { showNotificationWidtFireDate() } } //立刻發送通知訊息 private func showNotificationNow(){ // 建立通知物件 let myNotification: UILocalNotification = UILocalNotification() // 通知訊息內容 myNotification.alertBody = "通知訊息測試使用 Now" // 時區Timezone設定 myNotification.timeZone = NSTimeZone.defaultTimeZone() // 將通知物件入通知排程 UIApplication.sharedApplication().scheduleLocalNotification(myNotification) } //15秒後發送通知訊息 private func showNotificationWidtFireDate(){ // 建立通知物件 let myNotification: UILocalNotification = UILocalNotification() // 通知訊息內容 myNotification.alertBody = "通知訊息測試使用 FireDate" // 通知訊息發送時的聲音 myNotification.soundName = UILocalNotificationDefaultSoundName // 時區Timezone設定 myNotification.timeZone = NSTimeZone.defaultTimeZone() // 10秒後發送訊息 myNotification.fireDate = NSDate(timeIntervalSinceNow: 15) // 將通知物件入通知排程 UIApplication.sharedApplication().scheduleLocalNotification(myNotification) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } //------------end------------
Objective-C 程式範例
標頭檔
//-----------start----------- // // ViewController.h // // // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end //------------end------------
程式檔
//-----------start----------- // // ViewController.m // // // #import "ViewController.h" #import <UIKit/UIKit.h> @interface ViewController () @end @implementation ViewController { UIButton *dyNotificationNowButton; UIButton *dyNotificationFireButton; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 註冊通知使用聲音、訊息文字,通知無分類 [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]]; // Notification Now Button // 寬度100 高度30 按鍵 dyNotificationNowButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 250, 60)]; dyNotificationNowButton.backgroundColor = [UIColor grayColor]; // 按下時文字變黑色 [dyNotificationNowButton setTitleColor:[UIColor blackColor] forState: UIControlStateHighlighted]; [dyNotificationNowButton.layer setMasksToBounds:TRUE]; // Button 設定文字內容 [dyNotificationNowButton setTitle:@"Notification Now" forState: UIControlStateNormal]; // Button圓角10 dyNotificationNowButton.layer.cornerRadius = 10.0; // Button位於X座標中心及Y座標200 dyNotificationNowButton.layer.position = CGPointMake(self.view.bounds.size.width/2, 200); // 按下後執行onClickButton [dyNotificationNowButton addTarget:self action:@selector(onClickButton:) forControlEvents:UIControlEventTouchUpInside]; // Button Tag =2 dyNotificationNowButton.tag = 1; // Notification Fire Button // 寬度100 高度30 按鍵 dyNotificationFireButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 250, 60)]; dyNotificationFireButton.backgroundColor = [UIColor greenColor]; // 按下時文字變黑色 [dyNotificationFireButton setTitleColor:[UIColor blackColor] forState: UIControlStateHighlighted]; dyNotificationFireButton.layer.masksToBounds = true; // Button 設定文字內容 [dyNotificationFireButton setTitle:@"Notification FireDate" forState: UIControlStateNormal]; // Button圓角10 dyNotificationFireButton.layer.cornerRadius = 10.0; // Button位於X座標中心及Y座標300 dyNotificationFireButton.layer.position = CGPointMake(self.view.bounds.size.width/2, 300); // 按下後執行onClickButton [dyNotificationFireButton addTarget:self action:@selector(onClickButton:) forControlEvents:UIControlEventTouchUpInside]; // Button Tag =2 dyNotificationFireButton.tag = 2; // 加入兩個功能Button [self.view addSubview:dyNotificationNowButton]; [self.view addSubview:dyNotificationFireButton]; } /* Button處理事件,這邊使用Tag判斷按下的是哪個按鍵 */ - (IBAction)onClickButton:(id)sender { UIButton *button = (UIButton*) sender; if (1 ==button.tag ) { [self showNotificationNow]; } else if (2 ==button.tag ) { [self showNotificationWidtFireDate]; } } //立刻發送通知訊息 -(void) showNotificationNow { // 建立通知物件 UILocalNotification *myNotification = [[UILocalNotification alloc] init]; // 通知訊息內容 myNotification.alertBody = @"通知訊息測試使用 Now"; // 時區Timezone設定 myNotification.timeZone = [NSTimeZone defaultTimeZone]; // 將通知物件入通知排程 [[UIApplication sharedApplication] scheduleLocalNotification:myNotification]; } //15秒後發送通知訊息 -(void) showNotificationWidtFireDate { // 建立通知物件 UILocalNotification *myNotification = [[UILocalNotification alloc] init]; // 通知訊息內容 myNotification.alertBody = @"通知訊息測試使用 FireDate"; // 通知訊息發送時的聲音 myNotification.soundName = UILocalNotificationDefaultSoundName; // 時區Timezone設定 myNotification.timeZone = [NSTimeZone defaultTimeZone]; // 10秒後發送訊息 myNotification.fireDate = [[NSDate alloc] initWithTimeIntervalSinceNow:15]; // 將通知物件入通知排程 [[UIApplication sharedApplication] scheduleLocalNotification:myNotification]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end //------------end------------