这个功能很久之前就做出来了,最近正好有空可以梳理一下。
首先,我们要知道对于iOS8的通知我们可以做些什么,目前我们可以在注册iOS8通知的时候给不同类型通知加入对应的Action方法,也就是说一些简单地操作不用打开app就可以完成。比如说社交类应用收到一条消息,可以直接利用Action回复“OK”、“收到”之类的快捷短语。 手机qq就曾经加入过两个表情作为快捷回复,不知为何后来的版本取消了。
这是注册通知的方法,利用保存好的快捷短语数组,来创建每个Action,不过通知以横幅显示的话最多只有2个快捷方法。
- (void)registerPushServiceWithSetting
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
NSArray *replyArr = [[NSUserDefaults standardUserDefaults] valueForKey:@"reply"];
NSMutableArray *actionsArr = [NSMutableArray array];
for (NSInteger i = 0; i < replyArr.count ; i ++) {
//Action方法
UIMutableUserNotificationAction *quickReply = [[UIMutableUserNotificationAction alloc]init];
quickReply.identifier = [NSString stringWithFormat:@"quickReply%ld",i];
quickReply.title = replyArr[i];
quickReply.activationMode = UIUserNotificationActivationModeBackground;
quickReply.authenticationRequired = NO;
[actionsArr addObject:quickReply];
}
//使用category来标记某一种通知类型,比如说收到消息
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
category.identifier = @"detail-reply";
//把Action方法加入相应的category
[category setActions:actionsArr forContext:UIUserNotificationActionContextMinimal];
[category setActions:actionsArr forContext:UIUserNotificationActionContextDefault];
UIUserNotificationSettings *set = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:category,nil]];
[[UIApplication sharedApplication] registerUserNotificationSettings:set];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
}
接收到远程通知后点击相应的Action会调用下面的方法
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
{
//通过identifier来判断点击的是哪个按钮,然后执行对应的操作
NSArray *arr = [[[NSUserDefaults standardUserDefaults] valueForKey:@"reply"] copy];
for (NSInteger i = 0; i < arr.count ; i ++) {
if ([identifier isEqualToString:[NSString stringWithFormat:@"quickReply%ld",i]]) {
NSLog(@"quickReply %ld",i);
}
}
}
我们可以在项目内部编辑自定义的快捷回复,在编辑好保存的时候不要忘记调用通知的注册方法重新注册,这样我们就可以随时改变接受到通知后的快捷回复了
//
// EditViewController.m
// NotificationQuicklyReplyDemo
//
// Created by yyp on 15/5/15.
// Copyright (c) 2015年 Mingdao. All rights reserved.
//
#import "EditViewController.h"
@interface EditViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) NSMutableArray *replyArr;
@property (assign, nonatomic) BOOL isEditing;
@end
@implementation EditViewController
- (id)init
{
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
self.title = @"自定义通知快捷回复";
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editAction)];
[self loadData];
}
- (void)editAction
{
self.isEditing = !self.isEditing;
if (self.isEditing) {
[self.tableView reloadData];
[self.tableView setEditing:self.isEditing animated:YES];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editAction)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addReplyAction)];
} else {
[self.tableView reloadData];
[self.tableView setEditing:self.isEditing animated:YES];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editAction)];
self.navigationItem.leftBarButtonItem = nil;
}
}
- (void)finishEdit
{
[[NSUserDefaults standardUserDefaults] setObject:self.replyArr forKey:@"reply"];
[self registerPushServiceWithSetting];
}
- (void)registerPushServiceWithSetting
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
NSArray *replyArr = [[NSUserDefaults standardUserDefaults] valueForKey:@"reply"];
NSMutableArray *actionsArr = [NSMutableArray array];
for (NSInteger i = 0; i < replyArr.count ; i ++) {
NSDictionary *dic = replyArr[i];
if ([dic[@"status"] isEqualToString:@"selected"]) {
UIMutableUserNotificationAction *quickReply = [[UIMutableUserNotificationAction alloc]init];
quickReply.identifier = [NSString stringWithFormat:@"quickReply%ld",i];
quickReply.title = dic[@"reply"];
quickReply.activationMode = UIUserNotificationActivationModeBackground;
quickReply.authenticationRequired = NO;
[actionsArr addObject:quickReply];
}
}
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
category.identifier = @"detail-reply";
[category setActions:actionsArr forContext:UIUserNotificationActionContextMinimal];
[category setActions:actionsArr forContext:UIUserNotificationActionContextDefault];
UIUserNotificationSettings *set = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:category,nil]];
[[UIApplication sharedApplication] registerUserNotificationSettings:set];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
}
- (void)loadData
{
self.replyArr = [[[NSUserDefaults standardUserDefaults] valueForKey:@"reply"] mutableCopy];
if (self.replyArr.count == 0) {
[self.replyArr addObject:@"OK"];
[self.replyArr addObject:@"收到"];
}
[self.tableView reloadData];
}
- (void)addReplyAction
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"输入您自定义的回复", @"输入您自定义的回复") message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
}];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"确定", @"确定") style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
UITextField *tf = alert.textFields.firstObject;
if (tf.text.length > 0) {
[self.replyArr addObject:tf.text];
[self.tableView reloadData];
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"取消", @"取消") style:UIAlertActionStyleCancel
handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.replyArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text = self.replyArr[indexPath.row];
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.replyArr removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSString *str = self.replyArr[fromIndexPath.row];
[self.replyArr removeObject:str];
[self.replyArr insertObject:str atIndex:toIndexPath.row];
}
@end
完整的Demo在 点击打开链接