Ask for User Permission to Receive Uilocalnotifications in iOS 8

Ask for User Permission to Receive UILocalNotifications in iOS 8

Since iOS 8 you need to ask user's permission to show notifications from your app, this applies for both remote/push and local notifications. In Swift you can do it like this,

Update for Swift 2.0

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Override point for customization after application launch.
if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:")))
{
let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = "INVITE_CATEGORY"
notificationCategory.setActions([replyAction], forContext: UIUserNotificationActionContext.Default)

//registerting for the notification.
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:[.Sound, .Alert, .Badge], categories: nil))
}
else
{
//do iOS 7 stuff, which is pretty much nothing for local notifications.
}
return true
}

Swift 3.2

if(UIApplication.instancesRespond(to: #selector(UIApplication.registerUserNotificationSettings(_:)))){
let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = "INVITE_CATEGORY"
notificationCategory.setActions([replyAction], forContext: UIUserNotificationActionContext.Default)

//registerting for the notification.
application.registerUserNotificationSettings(UIUserNotificationSettings(types:[.sound, .alert, .badge], categories: nil))
}
else{
//do iOS 7 stuff, which is pretty much nothing for local notifications.
}

Objective C syntax is also very similar.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
// Override point for customization after application launch.
return YES;
}

To check for currently registered notification types you can use UIApplication class's method,

- (UIUserNotificationSettings *)currentUserNotificationSettings

So if the user has said no to your app then this function should return a setting without any types in it.

I have written a tutorial about this, you could see it here.

Ask for Permission for Local Notifications in iOS 8, but still have the App Support iOS 7

The following answer makes a few assumptions:

  1. The app must build properly with a Base SDK of iOS 8 when using Xcode 6 and it must build properly with a Base SDK of iOS 7 when using Xcode 5.
  2. The app must support a Deployment Target of iOS 7 (or earlier) regardless of the Base SDK and Xcode version.

Code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// None of the code should even be compiled unless the Base SDK is iOS 8.0 or later
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
// The following line must only run under iOS 8. This runtime check prevents
// it from running if it doesn't exist (such as running under iOS 7 or earlier).
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
#endif
}

All of this is covered in the Apple SDK Compatibility Guide.

iOS 8 wait for local notification permission

On iOS 8 and later, implement the following method in your AppDelegate:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {

// You could check here [[UIApplication sharedApplication] currentUserNotificationSettings]

// This is where you set up your local notification
}

This method is a UIApplicationDelegate method, so all you need to do is implement it in your MyAppDelegate.m file.

From the documentation:

Tells the delegate what types of notifications may be used to get the user’s attention.

Parameters

application: The app object that registered the user notification settings.

notificationSettings: The user notification settings that are available to your app.

The settings in this object may be different than the ones you originally requested.

Discussion

Apps that use local or remote notifications to alert the user to new information must register the types of notifications they want to use by calling the registerUserNotificationSettings: method of the app object. (In apps that link on versions of iOS prior to 8.0, registration can also happen implicitly when you schedule a local notification.) Your app’s request is combined with the user’s current preferences to determine what notification types are allowed and the results are delivered to this method in the notificationSettings parameter.

The first time you register your app’s preferred notification types, the system asks the user whether your app should be allowed to deliver notifications and stores the user’s response. The system does not prompt the user during subsequent registration attempts. The user can always change the notification preferences using the Settings app.

Because the user’s preferences can change, you should always check the contents of the notificationSettings parameter. These settings control only whether the user is notified about a local or remote notification. The notification is still delivered to your app at the appropriate times.

Do Local Notifications need user permission on iOS?

NOTE: this includes push notifications / remote notifications

when using Xcode6 with iOS7 or iOS8
Test when registerUserNotificationSettings: API is available at runtime.

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {  
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

Thanks to http://corinnekrych.blogspot.ae/2014/07/how-to-support-push-notification-for.html

How do I ask the user for permission to send local notifications after initial launch?

I ended up figuring it out! Here's what I did:

var body: some View {
if !hasPressedNotifButton {
Button(action: {

// Request permission to send notifications
self.center.requestAuthorization(options: [.alert, .sound])
{ (granted, error) in

// Hide this button by setting this @State variable to true
self.hasPressedNotificationsButton = true

if granted {
// Edit the user's data for later use
self.userData.wantsNotifications = true
}
}
}) {
Text("Set Notifications")
}
if self.userData.wantsNotifications {
WantsNotifsView()
} else {
NoNotifsView()
}
}
}

If anyone needs any clarification on how this works, please let me know! :)

Check if Local Notifications are enabled in IOS 8

You can check it by using UIApplication 's currentUserNotificationSettings

if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){ // Check it's iOS 8 and above
UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

if (grantedSettings.types == UIUserNotificationTypeNone) {
NSLog(@"No permiossion granted");
}
else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){
NSLog(@"Sound and alert permissions ");
}
else if (grantedSettings.types & UIUserNotificationTypeAlert){
NSLog(@"Alert Permission Granted");
}
}

Hope this helps , Let me know if you need more info

Error when trying to ask for user permission for notification

UIUserNotificationSettings is available in iOS 8.0 and later: UIUserNotificationSettings Class Reference

Your code crashes because the API is not available in iOS 7.

Check if user allowed local notifications or not. iOS 8. Obj-C

Here's what I use for less specific situations:

+ (BOOL)notificationsEnabled {
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
return settings.types != UIUserNotificationTypeNone;
}

I usually keep a set of these types of methods in a notification manager.



Related Topics



Leave a reply



Submit