简体   繁体   English

iOS-从后台处理远程推送通知。 前景

[英]iOS - Handling Remote Push Notifications From Background Vs. Foreground

I need to handle remote push notifications based on when I get them. 我需要根据收到通知的时间来处理远程推送通知。 When the app is in the background or terminated and if I get a push notification, and when I tap the push notification, I handle that in ` 当应用程序在后台运行或终止时,如果我收到推送通知,并且点击推送通知,则可以在“

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

where I can tell the visible UIViewController to push to the necessary view. 在这里我可以告诉可见的UIViewController推送到必要的视图。

However, when I receive a remote push notification while I'm already inside the app, I don't want the app to do anything but increment the notifications counter on the home view. 但是,当我已经在应用程序中时收到远程推送通知时,我不希望该应用程序执行任何操作,而是增加主视图上的通知计数器。 Right now it does the same thing, which pushes to the necessary view, but this should only be happening when the app was at first in the background and the user tapped the push notification to enter the app. 现在,它执行相同的操作,这会推送到必要的视图,但这仅应在应用程序最初位于后台并且用户轻按推送通知以进入应用程序时才发生。

How do I differentiate between these two use cases? 如何区分这两个用例?

You can check the "applicationState" property of UIApplication class. 您可以检查UIApplication类的“ applicationState”属性。

UIApplicationState applicationState = [UIApplication sharedApplication].applicationState;

if(applicationState==UIApplicationStateActive){
NSLog(@"push message received when app was active"):
}else{
}

You can do this check in the below delegate method which you already use. 您可以在下面已经使用的委托方法中进行检查。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

Hope it helps!! 希望能帮助到你!!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM