標籤:ios 本地通知
應用程式代理程式
//// BeyondAppDelegate.m// 35_本地通知//// Created by beyond on 14-9-12.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "BeyondAppDelegate.h"@implementation BeyondAppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ UITextView *text = [[UITextView alloc]init]; text.frame = CGRectMake(40, 0, 240, 300); text.backgroundColor = [UIColor redColor]; text.text = [NSString stringWithFormat:@"didLaunchWithOption\n%@", launchOptions]; [self.window.rootViewController.view addSubview:text]; // 1、如果點擊表徵圖進入應用程式,則launchOptions字典為nil // 2.1、如果點擊本地通知進行應用程式,則launchOptions字典有值,值為: /* ...... */ // 2.2、如果是其他應用程式跳轉到本應用 從而開啟本應用,則launchOptions字典有值,其值為: NSLog(@"didLaunchWithOption"); return YES;}/** 下面方法didReceiveLocalNotification,被調用的前提是: 1、應用沒有關閉,仍在後台運行,或者正在顯示中 2、使用者點擊通知, 進入了程式(程式還在運行中, 程式並沒有被關掉) 3、如果應用正在顯示,接收到通知時,依然會調用app代理的此方法 */- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ UILabel *label = [[UILabel alloc] init]; label.numberOfLines = 0; label.frame = CGRectMake(0, 300, 320, 30); label.backgroundColor = [UIColor blueColor]; label.text = @"didReceiveLocalNotification"; [self.window.rootViewController.view addSubview:label]; NSLog(@"didReceiveLocalNotification");}@end
控制器
//// BeyondViewController.m// 35_本地通知//// Created by beyond on 14-9-12.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "BeyondViewController.h"@interface BeyondViewController ()// 添加一個本地通知- (IBAction)addLocalNotice;// 取消本地通知- (IBAction)cancelLocalNotice;@end@implementation BeyondViewController// 添加一個本地通知- (IBAction)addLocalNotice{ // 1.建立通知 UILocalNotification *localNote = [[UILocalNotification alloc] init]; // 2.設定本地通知的屬性 localNote.alertAction = @"操作標題"; // 操作標題 localNote.alertBody = @"倫家想你啦~~~"; // 本文 localNote.applicationIconBadgeNumber = 5; // 本地通知的重複時間的間隔:每分鐘,每天,每小時... // localNote.repeatInterval = NSCalendarUnitMinute; // 點擊【本地通知】後, 開啟程式時,將會展示的啟動圖片 localNote.alertLaunchImage = @"Default.png"; // 【本地通知】幾時啟動 localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; // 3.給app 註冊一個【本地通知】(添加),必須先移除舊的,防止重複添加 UIApplication *app = [UIApplication sharedApplication]; [app cancelAllLocalNotifications]; [app scheduleLocalNotification:localNote];}// 取消本地通知- (IBAction)cancelLocalNotice{ UIApplication *app = [UIApplication sharedApplication]; [app cancelAllLocalNotifications];}@end
開啟通知,然後完全退出程式,點擊【本地通知】進入程式時,
只會調用didFinishLaunchingWithOptions方法,並傳參數 :launchOptions
不會調用didReceiveLocalNotification,
因為此方法調用的前提是:
應用正在運行,或仍在後台運行時,收到了通知
iOS_35本地通知