Lors de mon intégration j'ai un UIPageViewController
contenant un écran d'amorçage à la fin pour autoriser les notifications. L'utilisateur appuie sur un bouton intitulé "Activer les notifications" et la boîte de dialogue d'autorisation des notifications apparaît. Comment puis-je accomplir cela?Comment puis-je demander l'autorisation de notifications dans un IBAction?
0
A
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]];
}
'UIUserNotificationSettings' est dépréciée dans iOS 10. Vous devez utiliser' UNNotificationSettings' à la place. https://developer.apple.com/documentation/uikit/uiusernotificationsettings – Mateusz