ios 푸시알림 권한요청 명령문 추가

부가 설명
푸시알림 권한이 승인되지 않으면 푸시알림 전송되지 않음.
Tags
Push Noti
ios
permission

원격 알림 등록

애플리케이션이 시작될 때 또는 적절한 시점에 원격 알림에 앱을 등록합니다.
아래 명령어를 통해 registerForRemoteNotifications를 호출하여 푸시알림을 받을 수 있도록 하자
 
AppDelegate.mapplication:didFinishLaunchingWithOptions: 메소드에 푸시알림 권한요청문 추가.

objective-c

if ([UNUserNotificationCenter class] != nil) { // iOS 10 or later // For iOS 10 display notification (sent via APNS) [UNUserNotificationCenter currentNotificationCenter].delegate = self; UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge; [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { // ... }]; } else { // iOS 10 notifications aren't available; fall back to iOS 8-9 notifications. UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; [application registerUserNotificationSettings:settings]; } [application registerForRemoteNotifications];

swift

if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS) UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in }) } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) } application.registerForRemoteNotifications()
 

결과

이제야 푸시알림이 제대로 수신된다.
notion image
notion image