从 Xcode 11.4 开始,可以在 iOS 模拟器上模拟推送通知。具体操作步骤如下:
import Foundation
import UIKit
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
registerPushNotification()
return true
}
// 注册通知
func registerPushNotification() {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
// Enable or disable features based on authorization.
}
}
}
{
"Simulator Target Bundle": "工程对应的BundleID",
"aps": {
"badge": 1,
"alert": {
"title": "通知标题",
"subtitle": "通知副标题",
"body": "通知内容,测试点击打开百度"
},
"sound":"default"
},
"route": {
"url":"https://www.baidu.com/"
}
}
xcrun simctl push booted 把你的push_test.apns文件拖上去
写在最后,苹果灵动岛的设计虽然很惊艳,但目前貌似还没有开放给开发者来适配,说是到2022年底左右会发布ActivityKit,那时候第三方App才可以逐步适配灵动岛以及锁屏界面上的自定义小组件。