ios8通知中心與恢復按鈕 Notification

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.
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章