I got a question about UNUserNotificationSettings. Is there any method that we can use to turn on/off notification(sound, alert, badge) in iOs 10+? In iOs 9 and below, I used registerUserNotificationSettings method to turn on sound, alert, badge in the app but can't do the same in iOs 10+. Any suggestion for my case?
Asked
Active
Viewed 342 times
1 Answers
2
for iOS 10, you can use method requestAuthorizationWithOptions
like this:
//iOS 10
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];
This is the definition of the parameter options
:
typedef NS_OPTIONS(NSUInteger, UNAuthorizationOptions) {
UNAuthorizationOptionBadge = (1 << 0),
UNAuthorizationOptionSound = (1 << 1),
UNAuthorizationOptionAlert = (1 << 2),
UNAuthorizationOptionCarPlay = (1 << 3),
} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

isaced
- 1,735
- 3
- 16
- 24
-
thank for your tip, but that method can't solve my problem – A.D Nov 15 '16 at 08:11
-
@A.D Do you want to be turn on/off after authorization? – isaced Nov 15 '16 at 08:21
-
yes, that's exactly what I want to do. I intended to use this method: [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { }]; but it just get settings status, cannot update settings – A.D Nov 15 '16 at 08:26
-
unfortunately, you can't turn on/off the Push Notifications for your app from the app code, but you can get the status to guide the user to open – isaced Nov 15 '16 at 08:35
-
reference: http://stackoverflow.com/questions/10493949/enable-disable-apple-push-notification-from-iphone-app – isaced Nov 15 '16 at 08:41