標籤:
蘋果在iOS8之後有了全新的通知機制,當我們收到推送的訊息時,或者鎖屏的時候,讓使用者快捷的對通知進行操作,實現的如下:
下面我們就來實現這個效果:
首先在頁面上添加一個按鈕,點擊按鈕發送本地通知,
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(100, 100, 100, 100); btn.backgroundColor = [UIColor yellowColor]; [self.view addSubview:btn]; [btn addTarget:self action:@selector(btnAct:) forControlEvents:UIControlEventTouchUpInside];
按鈕實現方法:
- (void)btnAct:(UIButton *)btn{ // 1. 建立訊息上要添加的動作,以按鈕的形式顯示 // 1.1 接受按鈕 UIMutableUserNotificationAction *acceptAction = [UIMutableUserNotificationAction new]; acceptAction.identifier = @"acceptAction"; // 添加標識 acceptAction.title = @"接受"; // 設定按鈕上顯示的文字 acceptAction.activationMode = UIUserNotificationActivationModeForeground; // 當點擊的時候啟動程式 // 1.2 拒絕按鈕 UIMutableUserNotificationAction *rejectAction = [UIMutableUserNotificationAction new]; rejectAction.identifier = @"rejectAction"; rejectAction.title = @"拒絕"; rejectAction.activationMode = UIUserNotificationActivationModeBackground; // 當點擊的時候不啟動程式 rejectAction.authenticationRequired = YES; // 需要解鎖才能處理,如果 rejectAction.activationMode = UIUserNotificationActivationModeForeground; 那麼這個屬性將被忽略 rejectAction.destructive = YES; // 按鈕事件是否是無法復原轉的 // 2. 建立動作的類別集合 UIMutableUserNotificationCategory *categorys = [UIMutableUserNotificationCategory new]; categorys.identifier = @"alert"; // 動作集合的標識 [categorys setActions:@[acceptAction, rejectAction] forContext:UIUserNotificationActionContextMinimal]; // 把兩個按鈕添加到動作的集合中,並設定上下文樣式 // 3. 建立UIUserNotificationSettings,並設定訊息的顯示類型 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]]; // 4. 註冊通知 [[UIApplication sharedApplication] registerForRemoteNotifications]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; // 我們使用本地通知為例,介紹如何顯示出通知欄 UILocalNotification *notification = [UILocalNotification new]; if (notification != nil) { notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2]; // 觸發時間 notification.alertAction = @"alertAction"; // 定義查看通知的操作名 notification.hasAction = YES; notification.alertBody = @"測試推送的快捷回複(通知的本文內容:武哥最帥)"; notification.soundName = UILocalNotificationDefaultSoundName; // 通知的背景聲 notification.applicationIconBadgeNumber = 1; // 強迫症們最討厭的表徵圖左上方那個小數字 notification.category = @"alert"; notification.userInfo = @{@"key1": @"value1", @"key2": @"value2"}; // 給通知綁定一些處理通知時需要的額外的資訊 // 根據觸發事件的配置展示通知訊息 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; // 立即發送通知到手機 // [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; } }
然後我們需要在AppleDelegate協議方法裡面做其他的事情:
// 成功註冊通知後,會有回調方法,方法寫在AppDelegate裡#pragma mark - 成功註冊通知後會調用此方法- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ // 成功註冊registerUserNotificationSetting: 後,回調方法 NSLog(@"成功註冊%@", notificationSettings);}#pragma mark - 收到本地推送後調用的方法- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ // 收到本地訊息後調用的方法 NSLog(@"收到本地訊息後調用的方法%@", notification); // 表徵圖上的數字減1 application.applicationIconBadgeNumber -= 1;}#pragma mark - 通知事件處理的回調方法- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{ // 在非App頁面收到本地訊息,下拉訊息會有快捷回複的按鈕,點擊後調用的方法,根據identifier來判斷點擊的是哪個按鈕,notification為訊息內容 NSLog(@"%@ --- %@", identifier, notification); if ([identifier isEqualToString:@"acceptAction"]) { NSLog(@"點擊了接收按鈕"); FirstViewController *firstVc = [[FirstViewController alloc]init]; UINavigationController *naVc = [[UINavigationController alloc]initWithRootViewController:firstVc]; self.window.rootViewController = naVc; }else{ NSLog(@"點擊拒絕的按鈕"); } // 處理完訊息後,最後一定要調用這個代碼塊 completionHandler();}
這樣就實現了...
iOS--本地通知快捷回複方式