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

LollipopKit 25天前 21

项目是使用的 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)
  • frqk 20天前
    引用 2
    没有 requestAuthorization 吧
  • 游客
    3
返回