Mobile/iOS

[iOS][Objective-C] 8.0부터 Notification에 버튼달기

out of coding 2017. 1. 5. 10:24

별로 쓸모는 없지만, 기획자도 잘 모르는 기능이지만 ㅎㅎ

카테고리를 설정하여서, 버튼을 넣어줄수 있습니다.

이렇게 될 경우, 노티를 눌러서 가는 경우와, 뭐 별도의 처리를 할수 있겠죠.


물론. 서버에서도 json data를 변경하여 주어야 합니다.

단말의 카테고리 값과 alert에 들어간 category 값을 동일하게 만들어주기만 하면 동작합니다.


소스는 다음과 같으며, 버튼은 여러개 넣어줄수 있습니다.

카테고리도 여러개 추가할수 있죠. 하지만 한번에 하나의 동작만 되는것.


NSMutableSet *categories = [[NSMutableSet alloc] init];


UIMutableUserNotificationAction *action1 = [UIMutableUserNotificationAction new];

action1.title = NSLocalizedString(@"확인하기", nil); // 버튼에 표시되는 이름

action1.identifier = @"button1"; // 버튼 이름

action1.activationMode = UIUserNotificationActivationModeForeground; // 눌렀을 경우의 모드

action1.authenticationRequired = NO;


UIMutableUserNotificationCategory *category1 = [UIMutableUserNotificationCategory new];

[category1 setActions:@[action1] forContext:UIUserNotificationActionContextDefault];

category1.identifier = @"action1"; // 카테고리의 이름


[categories addObject:broadcastTimeCategory]; // 카테고리 리스트에 포함.

    

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:categories];

[UIApplication sharedApplication] registerUserNotificationSettings:settings];


나머지 부분들은 옵션을 한 번 조정해보세요.

서버쪽에서 보내는 예시는 넣지 않겠습니다. 참조하여 찾아보면 많음.