iOS开发之推送

一、苹果推送原理

从上图我们可以看到:
1、应用程序注册消息推送。
2、iOS从APNs获取device Token,应用程序接收device Token。
3、应用程序将device token发送给PUSH服务端程序。
4、服务端程序向APNS服务发送消息。
5、APNS服务将消息发送给iPhone应用程序。

二、本地推送步骤

  1. 先注册一个本地通知:
public func setNotifiAuth() {
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
            if granted == true {
                print("允许通知")
            } else {
                print("不允许通知")
            }
        }
    }
  1. 添加一个本地通知
public func send(_ body: String) {
        let content = UNMutableNotificationContent()
        content.title = "本地推送消息"
        content.subtitle = "******"
        content.body = body
        content.sound = UNNotificationSound.default
        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 0.1, repeats: false)
        let request = UNNotificationRequest.init(identifier: "req_identifier", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request)

    }
  1. 移除通知
// 移除一个通知
    public func removeOneNotification(_ noticeId: String) {
        let center = UNUserNotificationCenter.current()
        center.getPendingNotificationRequests { requests in
            requests.forEach { req in
                print("存在的ID:\(req.identifier)\n")
            }
        }
        print("移除currentID:\(noticeId)")
        center.removePendingNotificationRequests(withIdentifiers: [noticeId])
    }
    
    // 移除所有通知
    public func removeAllNotification() {
        let center = UNUserNotificationCenter.current()
        center.removeAllDeliveredNotifications()
    }

三、 远程推送

  1. 服务端生成APNS pem证书
  2. 开启Xcode远程推送功能
  3. 注册设备ID
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值