Ask for User Permission to Receive UILocalNotifications in iOS 8

前端 未结 5 677
自闭症患者
自闭症患者 2020-11-29 15:54

I have set up local notifications in the App Delegate Using this:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UILocalNotificati         


        
5条回答
  •  天命终不由人
    2020-11-29 16:27

    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.

提交回复
热议问题