- 先运行项目启动模拟器,然后终端运行如下命令:
xcrun simctl push --help
复制代码
如果出现如下信息表明可以测试远程推送
Send a simulated push notification
Usage: simctl push <device> [<bundle identifier>] (<json file> | -)bundle identifier
The bundle identifier of the target application
If the payload file contains a 'Simulator Target Bundle' top-level key this parameter may be omitted.
If both are provided this argument will override the value from the payload.
json file
Path to a JSON payload or '-' to read from stdin. The payload must:
- Contain an object at the top level.
- Contain an 'aps' key with valid Apple Push Notification values.
- Be 4096 bytes or less.
Only application remote push notifications are supported. VoIP, Complication, File Provider, and other types are not supported.
复制代码
- 然后在AppDelegate加上获取推送权限的代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let options:UNAuthorizationOptions = [.alert,.sound]
UNUserNotificationCenter.current().requestAuthorization(options: options) { (success, error) in
if let info = error?.localizedDescription {
print(info)
}
}
return true
}
复制代码
- 在桌面创建一个playload.json文件
{
"aps":{
"alert":{
"title":"测试",
"subtitle":"远程推送",
"body":"这是一条从远处而来的通知"
},
"sound":"default",
"badge":1
}
}
复制代码
- 运行如下命令即可
xcrun simctl push booted com.test.SwiftDemo /Users/roy/Desktop/playload.json
复制代码
com.test.SwiftDemo 是项目的bundle id
/Users/roy/Desktop/playload.json 是json文件的位置