iOS8.0人性化的“通知中心”

1、注册通知样式

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

//    1.创建消息上面要添加的动作(按钮的形式显示出来)

    UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];

    action.identifier = @"action";//按钮的标示

    action.title=@"Accept";//按钮的标题

    action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序

    //    action.authenticationRequired = YES;

    //    action.destructive = YES;

    

    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按钮

    action2.identifier = @"action2";

    action2.title=@"Reject";

    action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理

    action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;

    action.destructive = YES;

    

//    2.创建动作(按钮)的类别集合

    UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];

    categorys.identifier = @"alert";//这组动作的唯一标示

    [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];

    

    

    

//    3.创建UIUserNotificationSettings,并设置消息的显示类类型

    UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];

    

    [[UIApplication sharedApplication] registerForRemoteNotifications];

    [[UIApplication sharedApplication] registerUserNotificationSettings:uns];

    

    

    UILocalNotification *notification = [[UILocalNotification alloc] init];

    

    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5];

    

    notification.timeZone=[NSTimeZone defaultTimeZone];

    

    notification.alertBody=@"测试推送的快捷回复";

    

    notification.category = @"alert";

    

    [[UIApplication sharedApplication]  scheduleLocalNotification:notification];

    

    

    

    //用这两个方法判断是否注册成功

    

    // NSLog(@"currentUserNotificationSettings = %@",[[UIApplication sharedApplication] currentUserNotificationSettings]);

    

    //[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

    

    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来判断点击的哪个按钮

}

效果图:

收到时 需轻下拉

 

向左滑动:

原文地址:https://www.cnblogs.com/swallow37/p/4962064.html