[英]How to handle push notification when app is in background but not suspended
My app receiving push notification, and showing appropriate info message for that. 我的应用程序接收推送通知,并显示相应的信息消息。 However when I'm clicking to the message, application becomes active but application didFinishLaunchingWithOptions is not getting called which is right i think, since the application is not suspended and it just resigns active. 但是,当我点击消息时,应用程序变为活动但应用程序didFinishLaunchingWithOptions未被调用,这是正确的,我认为,因为应用程序未被暂停,它只是暂停活动。 The question is how i can make sure that user clicked to message when application becomes to foreground ? 问题是我如何确保用户在应用程序变为前台时单击消息?
I think what you are looking for is this app delegate method: 我认为你要找的是这个app delegate方法:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
It will be called if your app is backgrounded, and the notification payload will be delivered in the userInfo dictionary. 如果您的应用程序是后台的,则会调用它,并且通知有效负载将在userInfo字典中传递。 This contrasts with the situation when the app is launched from cold start, when this method does not get called, and instead you check in the launchOptions dictionary for the payload. 这与从冷启动启动应用程序时的情况形成对比,此时此方法未被调用,而是您在launchOptions字典中检查有效负载。
However the preferred way to do this since iOS7 is to use this: 但是,自iOS7以来这样做的首选方法是使用它:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
This method is called when a user taps on a notification, regardless of whether the app is launched from cold start or foregrounded from background. 无论应用程序是从冷启动启动还是从后台运行,都会在用户点击通知时调用此方法。 So even if you are not using the completionHandler, it provides a more consistent way of accessing the notification payload. 因此,即使您没有使用completionHandler,它也可以提供更一致的方式来访问通知负载。 If this method is present, the older one does not get called. 如果存在此方法,则不会调用较旧的方法。
If I understand the question correctly, you are asking how to be sure that the app was brought into the foreground as the result of the user “clicking” ie acting on a push notification . 如果我正确理解了这个问题,那么你就会问如何确保应用程序因为用户“点击”而被置于前台,即作用于推送通知 。
When the app is not running at all, you can use -application:didFinishLaunchingWithOptions:
as you mention. 当应用程序根本没有运行时,您可以使用-application:didFinishLaunchingWithOptions:
如您所述。 The launchOptions dictionary contains the payload, etc. — I won't describe this since you already know how this works. launchOptions字典包含有效负载等。 - 我不会描述这个,因为你已经知道它是如何工作的。
When the app IS running however, that method is not going to be called. 但是,当应用程序运行时,不会调用该方法。 Instead, - application:didReceiveRemoteNotification:
is called. 相反, - application:didReceiveRemoteNotification:
被调用。 BTW this is called if the app was already in the foreground OR if it was in the background and the user “clicked” on the push notification banner/alert to open the app. 顺便说一句,如果应用程序已经在前台,或者如果它在后台并且用户“点击”推送通知横幅/警报以打开应用程序,则会调用此方法。 It will not be called otherwise: so I believe this is exactly what you're looking for. 它不会被称为其他:所以我相信这正是你正在寻找的。
The userInfo dictionary provided by this method will contain the notifications data, similarly to -application:didFinishLaunchingWithOptions:
for more ad-hoc processing. 此方法提供的userInfo字典将包含通知数据,类似于-application:didFinishLaunchingWithOptions:
用于更多临时处理。 (Note: userInfo
and launchOptions
are semantically different, but hopefully this is obvious. :)) (注意: userInfo
和launchOptions
在语义上是不同的,但希望这很明显。:))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.