ask user for permission to show alert when firing local notification ask user for permission to show alert when firing local notification xcode xcode

ask user for permission to show alert when firing local notification


add this code, it will show a alert view to ask user for permission.

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge                                                                                                          categories:nil]];}

you can add this code in application:didFinishLaunchingWithOptions; method, so that the app will ask your user when they launch the app, or you can add this code when you set Local Notification, it's up to you.


蘇健豪's answer is good.

In Swift it looks like this:

let registerUserNotificationSettings = UIApplication.instancesRespondToSelector("registerUserNotificationSettings:")if registerUserNotificationSettings {     var types: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound         UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotific‌​ationSettings(forTypes: types, categories: nil))} 

Also see here: Ask for User Permission to Receive UILocalNotifications in iOS 8


//register notificationsif([application respondsToSelector:@selector(registerUserNotificationSettings:)]) //ios 8+{    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];    [application registerForRemoteNotifications];}else // ios 7 or less{    [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge];}