从推送通知更新sqlite数据库,应用程序已关闭,不在后台运行iOS
问题描述:
我试图更新本地sqlite数据库,当收到通知和应用程序关闭/用户死亡。当应用程序处于后台或活动模式时,一切正常。从推送通知更新sqlite数据库,应用程序已关闭,不在后台运行iOS
这里是代码我努力:
-(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);
}
}
问题:为什么它不是在应用程序不活动状态的工作。有没有解决这个问题的方法?
- (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
{
}
}
答
- (空)应用程序:(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);
}
当用户打开应用程序点击通知时,它正在工作。不工作的情况是:应用程序死亡,收到通知,用户打开应用程序使用应用程序图标不是通知,然后没有sqlite更新 –
您是否在didFinishLaunchWithOptions方法中使用上述代码? –
是的...请检查上面更新的问题。如果我做错了任何事,请纠正我。 –