Répondre

1

Vous pouvez mettre:

Objective-C

UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; 
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) 
         completionHandler:^(BOOL granted, NSError * _Nullable error) { 
          // Enable or disable features based on authorization. 
         }]; 
[[UIApplication sharedApplication] registerForRemoteNotifications]; // you can also set here for local notification. 

Swift

let center = UNUserNotificationCenter.current() 
    center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in 
     // Enable or disable features based on authorization. 
    } 
    UIApplication.shared.registerForRemoteNotifications() // you can also set here for local notification. 

dans votre IBAction.

S'il vous plaît rappelez-vous aussi ajouter import UserNotifications pour Swift ou #import <UserNotifications/UserNotifications.h> pour Objective-C dans le fichier où vous avez IBAction et assurez-vous que Push Notification est activé sous target-Capabilities - Push notification.

0

Objective-C:

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { 
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]; 
} 
+0

'UIUserNotificationSettings' est dépréciée dans iOS 10. Vous devez utiliser' UNNotificationSettings' à la place. https://developer.apple.com/documentation/uikit/uiusernotificationsettings – Mateusz