LollipopKit
V2EX  ›  iOS

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

  •  
  •   LollipopKit · Apr 13, 2021 · 2269 views
    This topic created in 1902 days ago, the information mentioned may be changed or developed.

    项目是使用的 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()
    }
    
    1 replies    2021-04-14 11:02:03 +08:00
    frqk
        1
    frqk  
       Apr 14, 2021 via iPhone
    没有 requestAuthorization 吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1008 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 18:15 · PVG 02:15 · LAX 11:15 · JFK 14:15
    ♥ Do have faith in what you're doing.