代码参考cocoachina上的
iOS8在本地通知的创建上引入了几个新的API:
UIUserNotificationType
UIUserNotificationSettings
UIUserNotificationCategory
UIUserNotificationAction
用法如下:
UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound;//通知的形式设定,分别有badge(桌面图标右上角的角标),alert,sound
//以下是设定iOS8通知出现时的几个button,此处用到了UIUserNotificationAction
UIMutableUserNotificationAction *justInformAction = [[UIMutableUserNotificationAction alloc] init];
justInformAction.identifier = @"justInform";
justInformAction.title = @"YES,I got it.";
justInformAction.activationMode = UIUserNotificationActivationModeBackground;
justInformAction.destructive = NO;
justInformAction.authenticationRequired = NO;
UIMutableUserNotificationAction *modifyListAction = [[UIMutableUserNotificationAction alloc] init];
modifyListAction.identifier = @"editList";
modifyListAction.title = @"Edit list";
modifyListAction.activationMode = UIUserNotificationActivationModeForeground;
modifyListAction.destructive = NO;
modifyListAction.authenticationRequired = YES;
UIMutableUserNotificationAction *trashAction = [[UIMutableUserNotificationAction alloc] init];
trashAction.identifier = @"trashAction";
trashAction.title = @"Delete list";
trashAction.activationMode = UIUserNotificationActivationModeBackground;
trashAction.destructive = YES;
trashAction.authenticationRequired = YES;
NSArray *actionArray = [NSArray arrayWithObjects:justInformAction,modifyListAction,trashAction, nil];
NSArray *actionArrayMinimal = [NSArray arrayWithObjects:modifyListAction,trashAction, nil];
//把上面的动作集成在一个category,只要用这个category就会在通知中出现这三个动作
UIMutableUserNotificationCategory *shoppingListReminderCategory = [[UIMutableUserNotificationCategory alloc] init];
shoppingListReminderCategory.identifier = @"shoppingListReminderCategory";//唯一标识符,在给通知指定category时就是通过这个来指定的
[shoppingListReminderCategory setActions:actionArray forContext:UIUserNotificationActionContextDefault];
[shoppingListReminderCategory setActions:actionArrayMinimal forContext:UIUserNotificationActionContextMinimal];
//将做好的category放进一个NSSet中并注册
NSSet *categoriesForSettings = [[NSSet alloc] initWithObjects:shoppingListReminderCategory, nil];
UIUserNotificationSettings *newNotificationSettings = [UIUserNotificationSettings settingsForTypes:type categories:categoriesForSettings];
UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (notificationSettings.types == UIUserNotificationTypeNone) {
[[UIApplication sharedApplication] registerUserNotificationSettings:newNotificationSettings];
}
通过上面的代码可以看出来新增的几个API都是为了帮助我们作成一个拥有唯一标识符的category并将其在系统中注册,然后在生成本地通知的时候通过这个唯一标识符为本地通知指定一个我们作好的category,理论上我们可以作成很多种category并在系统中注册保存下来。
下面的代码就是为我们的通知指定category:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = notificationDate;
localNotification.alertBody = @"Hey, you must go shopping, remember?";
localNotification.alertAction = @"View List";
localNotification.category = @"shoppingListReminderCategory";//指定category
localNotification.applicationIconBadgeNumber++;//本地通知出现时角标+1
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.category = @"shoppingListReminderCategory";
这里的category是iOS8新增的一个属性,与UIUserNotificationCategory配合使用。
收到通知后的逻辑操作在appdelegate的两个委托方法中完成:
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler{
if ([identifier isEqualToString:@"editList"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"modifyListNotification" object:nil];
} else if ([identifier isEqualToString:@"trashAction"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"deleteListNotification" object:nil];
}
[UIApplication sharedApplication].applicationIconBadgeNumber--;
completionHandler();
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
[UIApplication sharedApplication].applicationIconBadgeNumber--;
}
源码下载请点击 这里