请教下 iOS 推送 AppDelegate. Swift 该怎么写,或者缺少了什么步骤

2021-04-13 21:11:02 +08:00
 LollipopKit

项目是使用的 flutter,第一次接触原生 ios native,请教各位。

已经进行了的操作:
1 、apple 开发者账户网页添加了 push notification 证书
2 、xcode 里 runner target 的 capability 也打开了推送
3 、如下修改了 AppDelegate.swift (参考的官方文档

但是真机测试并没有 print 出 token 值,就很疑惑,想请教是不是缺少了什么步骤,还是 AppDelegate.swift 写错了

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(_ application: UIApplication,
                                didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        UIApplication.shared.registerForRemoteNotifications()
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    override func application(_ application: UIApplication,
                                didRegisterForRemoteNotificationsWithDeviceToken
                                deviceToken: Data) {
        print(deviceToken)
        sendDeviceTokenToServer(token: deviceToken)
    }
}

func sendDeviceTokenToServer(token: Data) {
    let url = URL(string: "https://push.example.com/ios?token=" + String(decoding: token, as: UTF8.self))!

    let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
        guard let data = data else { return }
        print(String(data: data, encoding: .utf8)!)
    }

    task.resume()
}
1424 次点击
所在节点    iOS
1 条回复
frqk
2021-04-14 11:02:03 +08:00
没有 requestAuthorization 吧

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/770435

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX