我想知道应该回调什么函数,或者当我的应用程序被挂起/终止时我收到通知。当我的应用程序处于前台/后台时,我能够处理,但是当应用程序被终止时,我不确定如何以及在哪里处理或解析我的通知有效负载?
从通知打开应用程序时,您将在应用程序中获取通知数据:将调用didFinishedLaunchingWithOptions方法。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions != nil)
{
// opened from a push notification when the app is closed
NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo != nil)
{
NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]);
}
}
else
{
// opened app without a push notification.
}
}
斯威夫特 5.1
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if launchOptions != nil {
// opened from a push notification when the app is closed
let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
if userInfo != nil {
if let object = userInfo?["aps"] {
print("userInfo->\(object)")
}
}
} else {
// opened app without a push notification.
}
}
您可以在应用程序被杀死时处理推送通知,请在您的
SceneDelegate
中使用以下方法
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let notificationResponse = connectionOptions.notificationResponse {
// get data from notification
let data = notificationResponse.notification.request.content.userInfo
// get specific key from that data
guard let feature = data["feature"] else {
return
}
}
}