1. UserNotifications framework 를 포함시킨다.
2. UserNotifications 헤더 파일을 포함시킨다.
#import <UserNotifications/UserNotifications.h>
3. UserNotifications 을 등록한다.
- 앱 시작시 등록해 주면 됩니다.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"requestAuthorizationWithOptions ok");
}
}];
4. notification 을 실행합니다.
- 앱이 background 인 경우만 동작하므로 active 인 경우에는 notifcation 을 보여줄 수 없습니다.
if( [UIApplication sharedApplication].applicationState != UIApplicationStateActive )
{
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"TestPushKit" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"New Call from 01012345678"
arguments:nil];
content.sound = [UNNotificationSound defaultSound];
// 배지를 변경하고 싶은 경우
//content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"New Call"
content:content trigger:nil];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"addNotificationRequest ok");
}
}];
}
else
{
NSLog(@"app is active");
}