简体   繁体   English

如何在应用程序处于后台但未暂停时处理推送通知

[英]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. :)) (注意: userInfolaunchOptions在语义上是不同的,但希望这很明显。:))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 iOS 应用程序被暂停/杀死并且用户点击通知时,如何处理 Firebase 推送通知? - How to handle Firebase push notification when iOS app is suspended/killed and user clicks on the notification? 退出或暂停应用程序时处理推送有效载荷 - Handle Push Payload when the App is Exited or Suspended 当APP处于后台但未挂起且APP打开时,推送通知无效 - Push notification dose not work when APP is in background but not suspended and when APP is open 在后台处理本地通知并暂停 - Handle local notification in background and suspended 当应用程序未运行或处于后台状态时,如何处理推送通知有效负载而不点击它 - How to handle push notification payload without tapping it when the app is in not running or background state 当应用程序在后台时推送通知 - Push notification when app in background 应用未运行/应用终止时如何处理推送通知 - How to handle push notification when app is not running/app is terminated 当应用在后台运行时无法处理推送通知,收到推送但未点击横幅或警报 - Can't handle Push Notification when the app is running in Background, receives push but didn't click on the banner or alert iOS如何在应用程序处于前台时处理解析JSON推送通知 - iOS How to Handle Parse JSON Push Notification when App is in Foreground 我的应用程序激活后如何处理推送通知? - How to handle push notification when my app become active?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM