检测从推送通知打开的应用程序(后台获取)

时间:2015-04-16 15:47:42

标签: ios objective-c notifications push

我使用UIBackgroundFetchResult捕获推送通知,如下面的代码...我还使用content availability = 1来检测后台模式

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)   (UIBackgroundFetchResult))completionHandler {

推送即将到来,并在应用处于活动状态或后台模式时始终执行 但是,当我打开推送时,我无法检测应用程序是否从推送打开,因为它总是进入状态

 if ( (application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground)  && (NotiType == 1 || NotiType == 2))
    {

}

1 个答案:

答案 0 :(得分:1)

您可以检查您的应用是否是从APNS(Apple推送通知服务)启动,来自应用委托方法

application:didFinishLaunchingWithOptions:

您可以在其中检查是否通过通知启动了应用

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (launchOptions != nil) {
          // Launched from push notification
          NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    }
}

如果您的应用在后台,则不会调用此方法。为此你可以用这种方式检查另一种方法,

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
    {
         //opened from a push notification when the app was on background
    }
}

HTH,享受编码!!