swift 友盟推送(umPush)

基于官方文档:

1. 推送配置
// 友盟推送配置
    func umPushConfig()  {
        // push组件基本功能配置
        let entity = UMessageRegisterEntity.init()
        //type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标
        entity.types = Int(UMessageAuthorizationOptions.badge.rawValue|UMessageAuthorizationOptions.sound.rawValue|UMessageAuthorizationOptions.alert.rawValue)
        if #available(iOS 10.0, *) {
            let action1 = UNNotificationAction.init(identifier: "action1_identifier", title: "打开应用", options: .foreground)
            let action2 = UNNotificationAction.init(identifier: "action2_identifier", title: "忽略", options: .foreground)
            //UNNotificationCategoryOptionNone
            //UNNotificationCategoryOptionCustomDismissAction  清除通知被触发会走通知的代理方法
            //UNNotificationCategoryOptionAllowInCarPlay       适用于行车模式
            let category1 = UNNotificationCategory.init(identifier: "category1", actions: [action1, action2], intentIdentifiers: [], options: .customDismissAction)
            let categories = NSSet.init(objects: category1)
            entity.categories = (categories as! Set)
            UNUserNotificationCenter.current().delegate = self
            UMessage.registerForRemoteNotifications(launchOptions: CSCConfig.sharedInstance.lauchOptions, entity: entity) { (granted, error) in
                if granted {
                    
                } else {
                    
                }
            }
            
        } else {
            // Fallback on earlier versions
            let action1 = UIMutableUserNotificationAction.init()
            action1.identifier = "action1_identifier"
            action1.title = "打开应用"
            action1.activationMode = .foreground
            let action2 = UIMutableUserNotificationAction.init()
            action2.identifier = "action2_identifier"
            action2.title = "忽略"
            action2.activationMode = .background //当点击的时候不启动程序,在后台处理
            action2.isAuthenticationRequired = true //需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
            action2.isDestructive = true
            let actionCategory1 = UIMutableUserNotificationCategory.init()
            actionCategory1.identifier = "category1" // 这组动作的唯一标示
            actionCategory1.setActions([action1, action2], for: .default)
            let categories = NSSet.init(objects: actionCategory1)
            entity.categories = (categories as! Set)
        }
    }
2. 获取deviceToken
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let device = NSData.init(data: deviceToken)
        let device_Token = device.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "").replacingOccurrences(of: " ", with: "")
        CLog(item: "deviceToken: \(device_Token)")
    }

你可能感兴趣的:(swift 友盟推送(umPush))