从推送通知更新sqlite数据库,应用程序已closures,不在后台iOS中运行

我正在尝试更新本地sqlite数据库收到通知和应用程序closures/用户终止。 一切正常,当应用程序是背景或主动模式。

参考: REFERENCE STACK LINK 1

参考堆栈链接2

这里是我正在尝试的代码:

-(void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{ NSLog(@"App Background :%@",userInfo); if(application.applicationState == UIApplicationStateInactive) { NSLog(@"Inactive"); //Show the view with the content of the push [self BackgrounCall:userInfo]; completionHandler(UIBackgroundFetchResultNewData); } else if (application.applicationState == UIApplicationStateBackground) { NSLog(@"Background"); //Refresh the local model [self BackgrounCall:userInfo]; completionHandler(UIBackgroundFetchResultNewData); } else { NSLog(@"Active"); //Show an in-app banner [self BackgrounCall:userInfo]; completionHandler(UIBackgroundFetchResultNewData); } } 

问题:为什么它不能在应用程序INACTIVE状态下工作。 有没有解决这个问题?

 - (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions { // Override point for customization after application launch. [[Database shareDatabase] createEditableCopyOfDatabaseIfNeeded]; 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"]); [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]]; } } else { } } 

– (void)应用程序:(UIApplication)应用程序didReceiveRemoteNotification:

只有当你的应用程序在后台时用户才会点击通知横幅,方法才会被调用。 如果你的应用程序被杀害,那么你需要处理横幅中的水龙头

didFinishLaunchWithOptions

method.You可以使用下面的代码来知道你是否点击横幅和应用程序被杀害。

 // Push Notification Handling [UIApplication sharedApplication].applicationIconBadgeNumber = 0; NSDictionary *pushDic = [[NSDictionary alloc]init]; pushDic = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]; if (pushDic) { NSLog(@"got push when app killed->%@",pushDic); }