iOS8的通知中心变得更加简洁直观,并提供自动删除通知等功能;而最值得称道的就是,当你在锁屏状态下向下拉出通知中心后,可以直接在其中回复收到的手机短信。操作也很简单,在“通知”一栏中向左滑动消息栏,即会弹出回复按钮。
实现通知的代码在AppDelegate.m文件中实现 具体代码如下:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//接收按钮
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
//按钮的标示
action.identifier = @"acceptAction";
//按钮的标题
action.title=@"查看消息";
//当点击的时候启动程序
action.activationMode = UIUserNotificationActivationModeForeground;
// action.authenticationRequired = YES;
// action.destructive = YES;
//第二按钮
UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
action2.identifier = @"rejectAction";
action2.title=@"Reject";
action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
action.destructive = YES;
//创建动作(按钮)的类别集合
UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
categorys.identifier = @"alert";//这组动作的唯一标示
[categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
//创建UIUserNotificationSettings,并设置消息的显示类型
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];
//注册推送
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
UILocalNotification *notification = [[UILocalNotification alloc] init];
//5.发起本地推送消息
notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5];
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.alertBody=@"测试推送的快捷回复";
notification.alertBody = @"censhi";
notification.category = @"alert";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
// Override point for customization after application launch.
return YES;
}
//本地推送通知
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//成功注册registerUserNotificationSettings:后,回调的方法
NSLog(@"%@",notificationSettings);
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//收到本地推送消息后调用的方法
NSLog(@"%@",notification);
}
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
//在非本App界面时收到本地消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮,notification为消息内容
NSLog(@"%@----%@",identifier,notification);
completionHandler();//处理完消息,最后一定要调用这个代码块
}
//远程推送通知
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
//向APNS注册成功,收到返回的deviceToken
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
//向APNS注册失败,返回错误信息error
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//收到远程推送通知消息
}
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
{
//在没有启动本App时,收到服务器推送消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}