Do Local Notifications Need User Permission on iOS

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! :)

Does setting icon badge number locally require user permission in iOS?

No, you can increase it anytime you want. This is not push notification feature so does not require user permissions.

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.

iOS Local Notifications: app is not asking for permissions to notify the user

I actually found the answer after Googling again (I didn't find the answer before)

Straight from Apple:

Resetting the Push Notifications Permissions Alert on iOS The first
time a push-enabled app registers for push notifications, iOS asks the
user if they wish to receive notifications for that app. Once the user
has responded to this alert it is not presented again unless the
device is restored or the app has been uninstalled for at least a day.

If you want to simulate a first-time run of your app, you can leave
the app uninstalled for a day. You can achieve the latter without
actually waiting a day by following these steps:

  1. Delete your app from the device.
  2. Turn the device off completely and
    turn it back on.
  3. Go to Settings > General > Date & Time and set the
    date ahead a day or more.
  4. Turn the device off completely again and
    turn it back on.

After doing this, asking for permission worked again.

Display UILocalNotification request after user clicked don't allow

You can send users to your app's settings page of user's device setting app, letting them to opt-in into LocalNotification

  • iOS earlier than version 10 dos not provide information if user has denied permission before or not.
  • In your app you need to save in NSUserDefaults or somewhere that you have already asked the permission.
  • Every time you require permission, first check if permission is available or not.
  • If permission is not available and your app has not asked user before ( based on the saved status in prior step ) you go for requesting permission.
  • If you have asked the permission from user before ( based on the saved status in prior step ) you prompt users if they want to allow permission, if they say 'Yes' forward them to the device setting app.

Objective-C for iOS <= 10 (Deprecated in iOS 10)

This is some code snip for inquiring UIUserNotificationSettings

UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]
if (currentSettings.types == UIUserNotificationTypeNone) // Permission does not exist either user denied or never been asked before

if (!(currentSettings.types & (UIUserNotificationTypeAlert | UIUserNotificationTypeSound))) // This means your app does not have permission alert and to play sounds with notification.

This code snip shows how to send users to device setting page.

NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}

Swift 3 for iOS <= 10 (Deprecated in iOS 10)

This is some code snip for inquiring UIUserNotificationSettings

let currentSettings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
if currentSettings.types.isEmpty {
// Here you decide whether to prompt user with new authorization request
// or send user to setting app based on your stored variable
}

This code snip shows how to send users to device setting page.

if let urlStr = URL(string: UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(urlStr) {
UIApplication.shared.openURL(urlStr)
}
}

For iOS 10 and above

instead of saving a variable in your app the check whether your app ever asked for authorization or not, check the value of authorizationStatus of UNNotificationSettings class

The value of this property is notDetermined if your app has never requested authorization using the requestAuthorization(options:completionHandler:) method.

along with other UNNotificationSettings counterpart methods similar to older version of this class ( UIUserNotificationSettings ) request permission or check for the permissions available like badge, alert or sound.

Local Notifications - asking for permission again after initially rejected

If for the first time you have disallowed local notification and on the second time use the blow mentioned code:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]
{
UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

if (grantedSettings.types == UIUserNotificationTypeNone)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app-settings:"]];
}
}

This will open your application settings where you can find the settings to enable/disable local notification and other settings.

Hope this helps!

Determine if local notifications have been explicitly denied

As @shpasta stated, you have to track this using NSUserDefaults. Using NSUserDefaults is very easy.

After calling registerUserNotificationSettings:, add this code:

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "didRegister")

Then, wherever you want to check for your current state of local notifications, use this:

if UIApplication.sharedApplication().currentUserNotificationSettings().types == TYPES_YOU_NEED {
// Code for enabled notifications
} else if NSUserDefaults.standardUserDefaults().boolForKey("didRegister") {
// Code for denied notifications
} else if !(NSUserDefaults.standardUserDefaults().boolForKey("didRegister")) {
// Code for non-enabled notifications, can display dialog
}

The only caveat with the above implementation is if the user only allowed some of the types you need, not all of them. You would check for that in the first condition, but I didn't add it in because I don't know what you want for that particular situation.



Related Topics



Leave a reply



Submit