UIKit-通知和推送

想要推送消息就需要添加
import UserNotifications包

申请权限

UNUserNotificationCenter.current()//获取UNUserNotificationCenter类的单例

requestAuthorization

requestAuthorization申请权限:
badge:角标
sound:声音
alert:横幅
carPlay:车载环境
criticalAlert:紧急通知
announcement:让耳机读取通知内容
无论用户是勿扰或静音状态都能通知 使用紧急通知需要给苹果写申请
providesAppNotificaitonSettings:在系统setting界面提供一个蓝色的app自定义选项
在这里插入图片描述

provisional:临时
发送一个不打断的notificaiton 的临时通知中心

        //发起权限申请
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge], completionHandler: {
            (granted, error) in
            if let error = error{
                print(error.localizedDescription)
            }
            if granted{
                print("用户已经授权")
            } else {
                print("通知服务器不要再发送了")
            }
        });

UIKit-通知和推送_第1张图片
这个授权通知只发送一次,除非删除app
如果用户第一次拒绝了通知,用户自己也可以通过
UIKit-通知和推送_第2张图片
自己打开权限

getNotificationSettings

会返回settings参数给闭包
options: [.alert,.sound,.badge]中的.alert是指lock screen,notification center,banners三个东西一一对应于settings.lockScreenSetting,settings.notificationCenterSetting,settings.alertSetting
每次发送通知之前都要获取一下状态再发送
UNNotificationSettings分三块
Getting authorization status:获取授权状态
getting device specific settings:获取设备通知配置
getting interface setting:获取通知界面配置

app只有在后台时才会接收到这个通知
在这里插入图片描述

当一个活动开始时activity kit 会从apple push notification service(apn)获得一个push token
push token对于请求的每一个活动是唯一的,这也是为什么你的app需要发送这个token给你的service在他发送push之前,无论如何你的service都必须发送token给apn,最后apn会将payload发送给设备,这将唤醒你的小控件来显示UI

modul

当权限被用户拒绝时,用户点击了发送通知,我们需要引导用户进入设置界面打开权限

                DispatchQueue.main.async {
                    let alert = UIAlertController(title: "alertViewTitle", message: "o", preferredStyle: .alert)
                    let cancleAction = UIAlertAction(title: "cancle", style: .cancel, handler: nil)
                    //引导user进入setting界面
                    let startingAction = UIAlertAction(title: "setting", style: .default) { action in
                        UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
                    }
                    alert.addAction(cancleAction)
                    alert.addAction(startingAction)
                    self.present(alert,animated: true, completion: nil)
                }

在通知下面添加按钮

app启动的时候就需要注册按钮,按钮是由整个App控制
UIKit-通知和推送_第3张图片

专业术语

APNs

apple push norification service

device token

唯一标识客户设备,可以看作寄快递的地址
需要先向apns注册app,苹果才会将用户的
我们需要先获取到用户的device token、,但是我们并不是直接找用户请求devic token而是找苹果,一开始要跟apns推送服务器连接起来,先向apns注册app,注册好后apns会将包含用户设备和包含特指app的信息组合成deviceToken发给app,因为deviceToken
然后将这个device token发送给苹果(APNs)告诉苹果我这个app需要给这个device token的用户发送远程通知
用户会将自己的device token发送给苹果(APNs)

证书和令牌

服务器个给apns发送信息的时候苹果想要链接是安全的链接,希望推送消息不要中途被拦截,篡改,希望app做一些安全处理,就可以做两种处理:

certificates

这个有一些限制 比如说每年要更新一次

token base (新东西)

给apns发送推送一般用这个,只需要创建一个以后就不需要再变了,也可以在多台服务器上使用

隐式推送

当包含.provisional的时候就为隐式推送 此时并不会弹出弹框询问用户权限,而是直接拥有权限,但是隐式推送只会显示在通知中心或者锁屏界面,并不会显示在主界面。

.requestAuthorization(options: [.provisional]

命令行转换音频

/System/Library/Sounds/Submarine.aiff …/Desktop/sub.caf -d ima4 -f caff -v

相关API

UNMutableNotificationContent

notification的内容
content.categoryIdentifier = “NOTIFICATION”//添加该notification的categoryIndentifier//主要决定了添加那个类型的button
content.badge = 1//忘了这里为什么为1了
content.userInfo = [“REQUEST_USER_ID”:88]
content.threadIdentifier = “NOTIFICATION”//主要给通知分组,当有多个不同的notification发来时会被折叠在两个notification里面如下图:
UIKit-通知和推送_第4张图片
2more这个东西叫做通知概要(summary)//但是感觉这两个被弃用了啊,目前看起来不启用
content.summaryArgument = “消息”
content.summaryArgumentCount = 1//默认是1,主要表示消息次数*n 这里是1

            //content.attachmzents//附件或视频

openURL

用于打开某个链接或者应用程序
在使用 canOpenURL: 和 openURL: 方法时,需要在应用的 Info.plist 文件中添加相关的白名单配置,以声明可以打开的 URL Scheme。

远程通知和本地通知的区别

本地通知是本地code写死了
远程通知可以不需要本地更新软件,就可以实时的从服务器推送通知
远程通知:
并不是app远程服务器直接发送给用户设备,而是远程服务器先把相关的推送内容(notification content,证书,目标用户的device token)发送给苹果(APNs),等APNs确认这些信息无误之后给指定的用户发送推送,内容格式使用的json格式

远程通知

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        //添加notification的代理
        UNUserNotificationCenter.current().delegate = self
        //找苹果注册我们这个app,苹果就会返回当前使用我们app的一个deviceToken,可以定位到这个具体的app在谁的手上
        UIApplication.shared.registerForRemoteNotifications()
        return true
    }
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("申请成功:\(deviceToken.map{ String(format: "%02.2hhx", $0)}.joined())")//获取到deviceToken,将deviceToken转化为16进制的字符串,可以用于模拟发送remoteNotification
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("申请失败:\(error.localizedDescription)")
    }

添加pushKit

    var window: UIWindow?
    var pushRegistry: PKPushRegistry!

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        UNUserNotificationCenter.current().delegate = self;
        
        
    // 设置 PushKit
        pushRegistry = PKPushRegistry(queue: nil)
        pushRegistry.delegate = self
        pushRegistry.desiredPushTypes = [.voIP]
        
        return true
    }

并且在
UIKit-通知和推送_第5张图片

你可能感兴趣的:(UIKit,ios)