iOS开发推送本地通知

一、LocalNotification的注册和处理。

要使用本地通知功能你需要现在AppDelegate中进行注册,声明通知的类型和相应的Action(如果有的话),代码如下:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        let completeAction = UIMutableUserNotificationAction()
        completeAction.identifier = "COMPLETE_TODO" // the unique identifier for this action
        completeAction.title = "Complete" // title for the action button
        completeAction.activationMode = .Background // UIUserNotificationActivationMode.Background - don't bring app to foreground
        completeAction.authenticationRequired = false // don't require unlocking before performing action
        completeAction.destructive = true // display action in red
       
        let remindAction = UIMutableUserNotificationAction()
        remindAction.identifier = "REMIND"
        remindAction.title = "Remind in 30 minutes"
        remindAction.activationMode = .Background
        remindAction.destructive = false
       
        let todoCategory = UIMutableUserNotificationCategory() // notification categories allow us to create groups of actions that we can associate with a notification
        todoCategory.identifier = "TODO_CATEGORY"
        todoCategory.setActions([remindAction, completeAction], forContext: .Default) // UIUserNotificationActionContext.Default (4 actions max)
        todoCategory.setActions([completeAction, remindAction], forContext: .Minimal) // UIUserNotificationActionContext.Minimal - for when space is limited (2 actions max)
       
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: NSSet(array: [todoCategory]) as? Set<UIUserNotificationCategory>))
        return true
    }

在Context为.Minmal的情况下,Action的展示顺序和你放入数组的顺序相反。

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void)

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)

两函数均位于AppDelegate,用于LocalNotification的处理,当弹出的通知被直接点击后第二个函数会被调用,你通过notification所携带的信息做出相应的操作;若注册时添加了Action,点击相应Action会调用第一个函数,你根据ActionIdentifier和notification来做出相应操作。

二、LocalNotification的设置。

UIApplication.sharedApplication().applicationIconBadgeNumber

上方变量用来设置应用图标红角标的数字,超过五位数中间会出现省略号。

        // create a corresponding local notification
        let notification = UILocalNotification()
        notification.alertBody = "Todo Item \"\(item.title)\" Is Overdue" // text that will be displayed in the notification
        notification.alertAction = "open" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view"
        notification.fireDate = item.deadline // todo item due date (when notification will be fired)
        notification.soundName = UILocalNotificationDefaultSoundName // play default sound
        notification.userInfo = ["title": item.title, "UUID": item.UUID] // assign a unique identifier to the notification so that we can retrieve it later
        notification.category = "TODO_CATEGORY"

        UIApplication.sharedApplication().scheduleLocalNotification(notification)

上述代码用来设置一个将于一段时间后触发的LocalNotification,触发时间由fireDate决定,携带的数据多保存在userInfo中。

你可能感兴趣的:(ios,IOS本地通知)