1、iOS8引入了地点通知(location notifications)。它其实也是本地通知(local notifications),但是他们只会在用户一个特定的地理或者iBeacon区域时,才会被触发。虽然我们看不到什么细节,地点通知 (location notifications)实现起来也很容易。
2、从iOS8开始,通知被加入了新的特性。简单地说,从现在开始,当一个通知被展示时,开发者可以指定用户可触发的具体的动作(actions),而且甚至不用启动App也可以处理这个通知。动作(Actions)可以被归类到一个类目(categories)下。特别是当App安排了不少通知显示的时候相当方便。用类目 (categories),一个通知所有相关的动作(actions)都可以一次性的被捆绑和指定。反之,处理动作(actions)也非常简单,只需要实现其代理方法即可。每一个动作(actions)都有一个特殊的属性标示符(identifier),被App用来辨别收到的动作(actions)然后适当地处理它。
3、可以被安排的本地通知的数量并不是无限的,最多有64个本地通知可以被安排和展示。如果多余这个数字,所有超过这个数字的通知都会被废弃。尽管如此,无论通知是以什么样的形式被安排的,最早的那个会被最先展示。
4、具体代码:
在ViewController.m中添加代码:
//创建通知
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//滑动提醒
localNotification.alertAction = @"查看消息";
localNotification.hasAction = YES;
//提醒内容
localNotification.alertBody = @"推送消息";
//category的identifier
localNotification.category = @"COMPLETE_CATEGORY";
//推送声音
localNotification.soundName = nil;
//触发时间
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
//应用程序右上角图标标记
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
在AppDelegate.m中添加代码:
在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中添加
UIMutableUserNotificationAction *notificationActionOk = [[UIMutableUserNotificationAction alloc]init];
notificationActionOk.identifier = @"completeRemindRater";
notificationActionOk.title = @"再工作一会儿";
//是否取消提醒
notificationActionOk.destructive = NO;
//是否需要权限,例如锁屏的时候,执行操作是否需要解锁再执行
notificationActionOk.authenticationRequired = NO;
//启动app还是后台执行
notificationActionOk.activationMode = UIUserNotificationActivationModeBackground;
notificationActionOk.behavior = UIUserNotificationActionBehaviorDefault;
UIMutableUserNotificationAction *notificationActionRestNow = [[UIMutableUserNotificationAction alloc]init];
notificationActionRestNow.identifier = @"relaxNow";
notificationActionRestNow.title = @"休息";
//是否取消提醒 当设置为yes时,通知中相应地按钮的背景色会变成红色
notificationActionRestNow.destructive = YES;
//是否需要权限,例如锁屏的时候,执行操作是否需要解锁再执行
notificationActionRestNow.authenticationRequired = NO;
//启动app还是后台执行
notificationActionRestNow.activationMode = UIUserNotificationActivationModeBackground;
notificationActionRestNow.behavior = UIUserNotificationActionBehaviorDefault;
UIMutableUserNotificationCategory *notificationCompleteCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCompleteCategory.identifier = @"COMPLETE_CATEGORY";
[notificationCompleteCategory setActions:@[notificationActionOk,notificationActionRestNow] forContext:UIUserNotificationActionContextMinimal];
[notificationCompleteCategory setActions:@[notificationActionOk,notificationActionRestNow] forContext:UIUserNotificationActionContextDefault];
UIMutableUserNotificationAction *replyAction = [[UIMutableUserNotificationAction alloc]init];
replyAction.title = @"回复";
replyAction.identifier = @"inline-reply";
replyAction.activationMode = UIUserNotificationActivationModeBackground;
replyAction.authenticationRequired = NO;
replyAction.behavior = UIUserNotificationActionBehaviorTextInput;
UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = @"REPLY_CATEGORY";
[notificationCategory setActions:@[replyAction] forContext:UIUserNotificationActionContextMinimal];
[notificationCategory setActions:@[replyAction] forContext:UIUserNotificationActionContextDefault];
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge categories:[NSSet setWithArray:@[notificationCompleteCategory,notificationCategory]]]];
实现协议方法:
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{
if ([identifier isEqualToString:@"completeRemindRater"]) {
NSLog(@"点击的是%@",identifier);
}
if ([identifier isEqualToString:@"relaxNow"]) {
NSLog(@"点击的是%@",identifier);
}
if ([identifier isEqualToString:@"inline-reply"]) {
NSLog(@"点击的是%@",identifier);
}
completionHandler();
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(@"接收到通知");
}
参考链接: