Nig*_*LxD 7 background-process push-notification ios ios-simulator swift
我一直在研究 iOS 中的后台执行。其中一种方法是无声通知。
无声通知(又名后台推送)可用于将应用程序从挂起状态唤醒并更新内容。从 Xcode 11.4 开始,可以模拟常规用户通知,如此处所述。然后,从 Xcode 14 开始,那些拥有 Apple Silicon Mac 笔记本电脑的用户将能够从 APNS 本身向模拟器发送通知......太棒了。
但这篇文章是关于无声通知的。
使用的 .apns 文件是,
{
"Simulator Target Bundle": "com.example.IOSBackgroundExec",
"aps": {
"content-available": 1
}
}
Run Code Online (Sandbox Code Playgroud)
在项目设置中,“签名和功能”选项卡下启用“远程通知”,并添加推送通知。无声通知不需要用户许可。
当应用程序被唤醒时,应该调用以下委托方法。
// Handling silent remote notification
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
NSLog(AppDelegate.TAG + "application(_:didReceiveRemoteNotification:fetchCompletionHandler:)")
// Some work
completionHandler(.newData)
}
Run Code Online (Sandbox Code Playgroud)
问题):
此外,如果您启用了远程通知后台模式,系统会启动您的应用程序(或将其从挂起状态唤醒),并在远程通知到达时将其置于后台状态。但是,如果用户强制退出,系统不会自动启动您的应用程序。
环境:Xcode 14.2、iOS 模拟器 16.x
到目前为止我得到的答案:
如果有人有不同的观察结果,请告诉我。
更新(来自评论):即使使用硅 Mac,也观察到后台推送不起作用。警报通知虽然有效。