Perfect(Server-Side Swift) + APNs

写在前面

Perfect[1]这家提供Swift服务端技术的公司,推出了Perfct Assistant(PA)[2]这款助手工具来更"Swift"地创建,开发,部署Swift服务器项目。👏👏
关于Perfect以及PA的任何疑问请登录Slack的中文频道[3]@rockford大神~
本文的原生教程请点传送门👉Perfect-Notifications👈

那么开始吧

话说在前几天刚在服务器上养了一只爬虫,让它帮我搜集一点东西。但是它翻出了宝贝也没法像🐶一样跑过来摇着尾巴要奖励,It's unfair!
所以给服务器加上了APNs,让它发现新的东西时候告诉我。

在开始以下工作之前请确保您已经配置好了APNs的证书


1.首先通过PA更新工程配置
使用PA很方便就能做好麻烦的配置工作。

�PA.png

2.服务器代码更新
此时在Xcode中左侧的文件目录部分就能看到一个PerfectNotifications的文件夹

Xcode.png

于是,新建一个ServerPush.Swift,初始化一个叫serverPush结构体单例 like this⬇️

 struct serverPush
 {
    //单例
    static let shared = serverPush()
    //私有属性configurationName
    private let configurationName = "moviePush"
 }

这么做是为什么呢?

  1. 点开上图NotificationPusher.Swift可以看见一段被注释的示例代码
  • 可以关注到,初始化的代码建议只执行一次,那么就放在单例里。


    code.png
  • 并且 configurationName随便写什么都没问题。

所以我们接着给serverPush一个private限定的初始化方法,不让外界能主动调用。内容就是那段示例的初始化的代码就好⬇️

private init()
{
       //setUp
        NotificationPusher.addConfigurationIOS(name: configurationName)
        {
            (net:NetTCPSSL) in
            
            // This code will be called whenever a new connection to the APNs service is required.
            // Configure the SSL related settings.
            
            net.keyFilePassword = "xxxxx"

            guard
                net.useCertificateChainFile(cert: "./ck.pem") &&
                net.useCertificateFile(cert: "./PushChatCert.pem") &&
                net.usePrivateKeyFile(cert: "./PushChatKey.pem") &&
                net.checkPrivateKey()
            else
            {
                print("Error validating private key file: \(net.errorStr(forCode: Int32(net.errorCode())))")
                return
            }
        }
        
        NotificationPusher.development = true //
}

这段代码里有几个值得注意的地方:

  • useCertificateFile需要的是下载APNs证书(就是那个名字叫aps_xxxx.cer的),把它在terminal.app中转成.pem的格式。
    x509 -inform der -in xxx.cer -out cert.pem
  • usePrivateKeyFile需要的是这个证书的.p12文件(可以在Launchpad中的Keychain中的对应证书中找到),同样转换成pem文件。
    openssl rsa -in xxx.pem -out key.pem
    在生成p12的过程中如果有设置密码,net.keyFilePassword就应该赋值为你设置的密码。
  • useCertificateChainFile看名字就知道了,需要上述两个文件合一之后的pem文件。
    cat xxx.pem xxx.pem > ck.pem

3.然后封装一下调用代码

 // BEGIN - individual notification push
    func beginPush()
    {
        let deviceId = ["hex string device id"]
        let ary = [IOSNotificationItem.alertBody("收到通知了,做点什么好呢?"), IOSNotificationItem.sound("default")]
        
        let n = NotificationPusher()
        n.apnsTopic = "com.company.my-app"
        n.pushIOS(configurationName: configurationName, deviceTokens: deviceId, expiration: 0, priority: 10, notificationItems: ary)
        {
            //没错误我就不关心了。
            _ = $0.map
            {
                if case .ok = $0.status{} else
                {
                    print("NotificationResponse: \($0.status)" + (String(data: Data(bytes: $0.body), encoding: .utf8) ?? ""))
                }
            }
        }
    }

同样需要注意的是:

  1. apnsTopic一定要和您创建的App bundle id 一样,要不然肯定推送失败。
  • deviceId填写您设备运行您创建的App中,注册通知方法返回的deviceToken,不要写成UUID之类的了。附上大家都写的差不多的教程😂
  • 好像没了

4."你听说吗,昨天我的爬虫会摇尾巴了" "WTF..."

push.png

所以。。该结束了?

服务器的推送功能到这里算是已经集成好了。Em..感觉离想象又近了一步。
Ps:今天听了一天的歌,又要到周末啦,祝各位周末快乐。


  1. https://www.perfect.org/

  2. https://www.perfect.org/en/assistant/

  3. https://perfectswift.slack.com/messages/china-developers/details/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容