【代码片-1】 swift 本地通知(二)iOS9.0

浏览: 67 发布日期: 2016-11-30 分类: swift
//
//  AppDelegate.swift
//  1119-noti
//
//  Created by targetcloud on 2016/11/19.
//  Copyright © 2016年 targetcloud. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        registerAuthor()
        
        // launchOptions: 点通知启动APP时会把对应的一些信息参数, 放到这个字典里面
        print("didFinishLaunchingWithOptions launchOptions \(launchOptions)")
        
        let tv = UITextView(frame: CGRect(x: 20, y: 20, width: 300, height: 300))
        tv.backgroundColor = UIColor.cyan
        tv.text = launchOptions?.description
        window?.rootViewController?.view.addSubview(tv)
        
        if launchOptions != nil {
            let local = launchOptions![UIApplicationLaunchOptionsKey.localNotification]
            if local != nil {
                print("didFinishLaunchingWithOptions local \(local)")
            }
        }
        return true
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        UIApplication.shared.applicationIconBadgeNumber = 0
    }
    
    func registerAuthor() {
        if #available(iOS 8.0, *) {
            let type = UIUserNotificationType.alert.rawValue | UIUserNotificationType.badge.rawValue | UIUserNotificationType.sound.rawValue
            
            let category: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
            category.identifier = "select"
            
            let action1 = UIMutableUserNotificationAction()
            action1.identifier = "wangzha"
            action1.title = "王炸"
            action1.activationMode = .foreground
            action1.isAuthenticationRequired = true
            action1.isDestructive = true
            
            let action2 = UIMutableUserNotificationAction()
            action2.identifier = "shunzi"
            action2.title = "顺子"
            if #available(iOS 9.0, *) {
                action2.behavior = .textInput
                action2.parameters = [UIUserNotificationTextInputActionButtonTitleKey: "压死"]
            }
            action2.activationMode = .background
            action2.isAuthenticationRequired = false
            action2.isDestructive = false
            
            let actions: [UIUserNotificationAction] = [action1, action2]
            category.setActions(actions, for: UIUserNotificationActionContext.default)
            let categories: Set<UIUserNotificationCategory> = [category]
            let sets = UIUserNotificationSettings(types: UIUserNotificationType(rawValue: type), categories: categories)
            UIApplication.shared.registerUserNotificationSettings(sets)
        }
    }
    
    // 9.0
    func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, withResponseInfo responseInfo: [AnyHashable: Any], completionHandler: @escaping () -> Void) {
        print("identifier, responseInfo \(identifier, responseInfo)")
        //identifier, responseInfo (Optional("wangzha"), [:])
        //identifier, responseInfo (Optional("shunzi"), [AnyHashable("UIUserNotificationActionResponseTypedTextKey"): 10JOKA])
        completionHandler()
    }
    
    // 前台状态或者从后台进入到前台(锁屏, 后台,用户点击了通知, 启动App)
    func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
        UIApplication.shared.applicationIconBadgeNumber = notification.applicationIconBadgeNumber
        print(">接收到通知")
        print(">UIApplication.shared.applicationState.rawValue \(UIApplication.shared.applicationState.rawValue)")
        let state = UIApplication.shared.applicationState
        if state == .active {
        }
        if state == .inactive {
        }
    }
}
返回顶部