Swift 清除通知栏指定消息(实现微信的视频通话通知消息,挂断

2018-08-08  本文已影响130人  GloryMan

清除通知栏指定推送

1,通知(UserNotifications)介绍

iOS10新增加了一个UserNotifications(用户通知框架)
来整合通知相关的API,UserNotifications框架增加了很多令人惊喜的特性:

UserNotifications 基本流程如下

请求权限

1.iOS 进行推送权限的申请

import UserNotifications

func registNoti() {
    UNUserNotificationCenter.current().delegate = self                 
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (accepted, error) in
            if !accepted {
                print("用户不允许消息通知")
            }
        }
    UIApplication.shared.registerForRemoteNotifications()
}

2.第一次启动会调用上面的方法,系统弹出窗口提示用户(如果用户拒绝,再次启动也不会弹出次窗口,也就不能收到通知消息)


11.03.03-w528

权限判断

        UNUserNotificationCenter.current().getNotificationSettings { (notiSettions) in
            switch notiSettions.authorizationStatus {
                case .authorized:
                    print("已授权")
                    break
                case .denied:
                    print("用户拒绝")
                    break
                case .notDetermined:
                    print("还没有请求授权窗口")
                    // 需要调用上面的请求授权的方法
                    break
            }
        }
        
更多请查看:

    open var soundSetting: UNNotificationSetting { get }

    open var badgeSetting: UNNotificationSetting { get }

    open var alertSetting: UNNotificationSetting { get }

    open var notificationCenterSetting: UNNotificationSetting { get }

    open var lockScreenSetting: UNNotificationSetting { get }

获取APNS: DeviceToken (重点)

这里需要引入一个新的概念 PusiKit 上面是PushKit 这里不做过多解释
需要注册Voip证书与普通推送证书在选择的时候选择voip


11.24.34-w418

1.注册PushKit

    import PushKit
    func registerPushNoti() {
        //注册push 推送
        let pushRegistry = PKPushRegistry.init(queue: nil)
        pushRegistry.delegate = self
        pushRegistry.desiredPushTypes = [PKPushType.voIP]
    }

2.启动App之后

// 普通的devicetoken 会在 Appdelegate 回调中返回
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        
    }

// 注册Voip推送之后devicetoken会在 PKPushRegistryDelegate 代理中回调

    func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
        let deviceToken = pushCredentials.token
        // 次devicetoken 是voip devicetoken 与 普通推送的devicetoken 不相同的
        print(deviceToken)
}

收到推送的处理(这里面就是更新通知、移除通知的操作)

1.接收远端的推送通知,获取推送内容


    func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {

        guard let dic = payload.dictionaryPayload as? JSON else {
           print("payload.dictionaryPayload as? JSON false")
            return
        }
        // 得带远端推送过来的推送信息(这时候手机是不会弹出通知栏提示用户的你需要在这个方法中写一个本地的推送消息)
        print(dic)                
}

2.使用本地推送,提示用户弹出本地推送(这里使用 UNUserNotificationCenter 也是 iOS 10 之后新出的通知管理)


    // 3.基于时间间隔的本地推送
    static func setTimeTrigger(content: UNMutableNotificationContent) {
        //设置5秒后触发
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(XBGlobalVariable.interval), repeats: true)
        //先创建一个请求(这里记住这个标识符,等会需要用到)
        let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger)
        //将请求添加到发送中心
        UNUserNotificationCenter.current().add(request) { error in
            if error == nil {
                print("Time Interval Notification scheduled: (TimeIntervalNotificationTrigger)")
            }
        }
    }
    // 返回一个  UNMutableNotificationContent 用于推送提示用户
    func setConfigConnect(title: String, subtitle: String) -> UNMutableNotificationContent{
        let content = UNMutableNotificationContent()
        content.title = title //推送内容标题
//        content.subtitle = subtitle //推送内容子标题
         content.body = subtitle // 推送内容body
        content.categoryIdentifier = "categoryIdentifier"  //category标识,操作策略
        content.sound = UNNotificationSound(named: "53.wav") // 音乐文件必须是aiff, wav, caf才可以
        let path = Bundle.main.path(forResource: "123", ofType: "png")
        let imageAttachment = try! UNNotificationAttachment(identifier: "iamgeAttachment", url: URL(fileURLWithPath: path!), options: nil)

        content.attachments = [imageAttachment]
        return content
    }

// 清除指定通知的方法 
UNUserNotificationCenter.current().removeDeliveredNotifications(["identifier"])

3.代码就是上面的这么多,下面说先流程

收到推送回调PushKit回调--->判断是拨打视频通话的推送--->弹出本地通知(标识符)--->收到挂断推送--->清除推送

上一篇下一篇

猜你喜欢

热点阅读