iOS8/9的Notification新特性

在iOS8和iOS9下,notification增加了一些新的特性,使之更加强大:
– iOS8增加了下拉时的Action按钮,像微信一样;
– iOS9增加了像信息一样的可以下拉直接输入;

iOS8为了实现action按钮,增加了4个类,一起完成这个功能:
– UIUserNotificationSettings;
– UIUserNotificationType:alert/sound/badge
– UIUserNotificationCategory;
– UIUserNotificationAction;

闲话少说,还是代码讲的清楚:

//初始化action
UIMutableUserNotificationAction* action1 = [[UIMutableUserNotificationAction alloc] init];
//设置action的identifier
[action1 setIdentifier:@”action1″];
//title就是按钮上的文字
[action1 setTitle:@”title1″];
//设置点击后在后台处理,还是打开APP
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
//是不是像UIActionSheet那样的Destructive样式
[action1 setDestructive:NO];
//在锁屏界面操作时,是否需要解锁
[action1 setAuthenticationRequired:NO];

UIMutableUserNotificationAction* action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setIdentifier:@”action2″];
[action2 setTitle:@”title2″];
[action2 setActivationMode:UIUserNotificationActivationModeForeground];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];

//一个category包含一组action,作为一种显示样式
UIMutableUserNotificationCategory* category = [[UIMutableUserNotificationCategory alloc] init];
[category setIdentifier:@”category1″];
//minimal作为banner样式时使用,*多只能有2个actions;default*多可以有4个actions
[category setActions:@[action1,action2] forContext:UIUserNotificationActionContextMinimal];

NSSet* set = [NSSet setWithObject:category];
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound
categories:set];
//注册notification设置
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

//添加一个notification,10秒后触发
UILocalNotification* notification = [[UILocalNotification alloc] init];
//设置notifiction的样式为”category1″
[notification setCategory:@”category1″];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
[notification setAlertBody:@”this is an alert”];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

结果是这个样子的:

这时候点击action1,alert就直接消失了,因为action1的setActivationMode是background的;而点击action2,app就会被打开;
无论哪个action,系统打开的时候都会调用AppDelegate的-(void)application: handleActionWithIdentifier: forLocalNotification:(nonnull UILocalNotification *)completionHandler:方法;如果直接点击alert的话,会调用application: didFinishLaunchingWithOptions:方法,而options参数里就包含了alert的信息;

几个要注意的地方:

push notification的时候,如何指定alert的样式,即category?
只需要在push的内容里添加category的Identifier:
“aps”:{
“alert”:”This is a notification”,
“category”:<category的Identifier>
}

APP正在运行的时候,收到了notification,如何接收?
此时会调用application:didReceiveLocalNotification:方法;而系统不会有任何操作,不会发出声音、添加badge icon、也不会弹出alert;

alert可以设置一个alertLaunchImage,可以替代系统默认的launch image;

APP的badge number如何自动+1:
苹果没有提供这个方法,因此我们要在服务器上保存某个用户的badge number,在推送时+1;

能不能像信息那样,下拉直接回复?
iOS8没有这个功能,*新的iOS9可以:

action.behavior = UIUserNotificationBehaviorTextInput;
1
iOS8时,增加了在指定地点弹出的功能:[notification setRegion:];

如何确定用户有没有禁止发送notifications:
设置notificationCategory后,检查AppDelegate有没有调用下面的方法:

– (void) application: didRegisterUserNotificationSettings: