1、簡介
本地通知是由本地應用觸發的,它是基於時間行為的一種通知形式,例如鬧鍾定時、待辦事項提醒,又或者一個應用在一段時候后不使用通常會提示用戶使用此應用等都是本地通知。
2、創建UILocalNotification
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. UILocalNotification *localNotifi = [UILocalNotification new]; localNotifi.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];//發送時間 localNotifi.alertBody = @"這是一條本地推送";//設置提醒內容 localNotifi.soundName = UILocalNotificationDefaultSoundName;//設置推送聲音 localNotifi.applicationIconBadgeNumber = 100;//設置app右上角圖標標記 // localNotifi.hasAction = YES;//鎖屏時是否顯示內容,默認yes 設置提醒按鈕文字 // localNotifi.alertAction = @"好的";//按鈕文字 localNotifi.timeZone = [NSTimeZone defaultTimeZone];//設置時區 [NSTimeZone defaultTimeZone],跟隨手機的時區 localNotifi.repeatInterval = NSCalendarUnitMinute;//設置重復每隔多久發送一次 最小單位分鍾 。0代表不重復,此屬性設置了, 那么調度池不會用完釋放!需要手動刪除通知對象 localNotifi.repeatCalendar = [NSCalendar calendarWithIdentifier:@"NSCalendarIdentifierChinese"];//設置依賴的日歷歷法,默認就是跟隨系統走,歷法不一樣每月重復間隔時間也不一樣(如農歷是30天) if (@available(iOS 8.2, *)) { localNotifi.alertTitle = @"本地推送呵呵"; } else { // Fallback on earlier versions }//設置彈出框標題 UIMutableUserNotificationCategory *category = [UIMutableUserNotificationCategory new];//使用可變子類 category.identifier = @"分類";//設置標識符,注意與發送通知設置的category標識符一致 // 設置前台按鈕,點擊后能使程序回到前台的叫做前台按鈕 UIMutableUserNotificationAction *actionLeft = [UIMutableUserNotificationAction new]; actionLeft.identifier = @"left"; actionLeft.activationMode = UIUserNotificationActivationModeForeground; actionLeft.title = @"前台按鈕"; // 設置后台按鈕,點擊后程序還在后台執行,如QQ的消息 UIMutableUserNotificationAction *actionRight = [UIMutableUserNotificationAction new]; actionRight.identifier = @"right"; actionRight.activationMode = UIUserNotificationActivationModeBackground; actionRight.title = @"后台按鈕"; [category setActions:@[actionLeft,actionRight] forContext:UIUserNotificationActionContextDefault]; NSSet *catogorySet = [NSSet setWithObject:category]; localNotifi.category = @"分類"; // 在哪個區域發送通知, 進入這個區域就發送這個通知,可以進來調一次,出去調一次 // @property(nullable, nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0); // @property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0);區域是否只檢測一次 // @property(nullable, nonatomic,copy) NSString *alertLaunchImage;設置啟動圖片 // @property(nullable, nonatomic,copy) NSDictionary *userInfo;推送攜帶參數 // @property (nullable, nonatomic, copy) NSString *category NS_AVAILABLE_IOS(8_0);添加下拉快速回復功能 if (iOS8_OR_LATER) { // UIUserNotificationType 枚舉: // UIUserNotificationTypeNone = 0, // UIUserNotificationTypeBadge = 1 << 0, //圖標標記 // UIUserNotificationTypeSound = 1 << 1, //聲音 // UIUserNotificationTypeAlert = 1 << 2, //提醒 // // categories:用於添加下拉快速回復功能 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:catogorySet]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; } // 調度本地推送通知(調度后在特定時間fireData發出) // [[UIApplication sharedApplication] scheduleLocalNotification:localNotifi]; // 立即發送本地通知 [[UIApplication sharedApplication] presentLocalNotificationNow:localNotifi]; // 處理退出后通知的點擊,程序啟動后獲取通知對象,如果是首次啟動還沒有發送通知,那第一次通知對象為空,沒必要去處理通知 if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) { UILocalNotification *localNotifi = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]; [self changeLocalNotifi:localNotifi]; } return YES; }
3、移除推送通知
- (void)deleteLocalNotifi{ // 刪除所有通知 [[UIApplication sharedApplication] cancelAllLocalNotifications]; // 刪除指定通知(發出過期的推送不在此數組) NSArray *notifiArr = [[UIApplication sharedApplication] scheduledLocalNotifications]; for (UILocalNotification *localNoti in notifiArr) { //根據UserInfo的值,來查看這個是否是你想要刪除的通知 if (localNoti.userInfo) { [[UIApplication sharedApplication] cancelLocalNotification:localNoti]; } } }
4、就收到推送處理方法
//處理通知 - (void)changeLocalNotifi:(UILocalNotification *)localNotifi{ // 如果在前台直接返回 if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { return; } // 獲取通知信息 // localNotifi.userInfo } //接收到本地通知后調用 程序前台或后台的時候才有用,退出無法接收到消息 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ [self changeLocalNotifi:notification]; }
5、前台和后台按鈕處理方法
//前台和后台按鈕處理方法 - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)(void))completionHandler{ // 我們可以在這里獲取標識符,根據標識符進行判斷是前台按鈕還是后台按鈕還是神馬按鈕,進行相關邏輯處理(如回復消息) NSLog(@"identifier : %@",identifier); // 一旦接受必須調用的方法(告訴系統什么時候結束,系統自己對內部進行資源調配) completionHandler(); }
6、遠程推送