apple-push-notifications - 在后台iOS中,从推送通知更新SQLite数据库,应用程序关闭且未运行

  显示原文与译文双语对照的内容
122 1

当收到通知和应用程序关闭/终止时,我正在尝试更新本地SQLite数据库。当应用程序的背景或者活动模式时一切都正常。

参考:参考堆栈链接 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);
 } 
 }

问题:为什么它不能在应用程序非活动状态下运行。这个问题有解决办法?

- (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
{ 
}
 }
时间:原作者:0个回答

100 3

- ( 空心) 应用程序:( 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); 
}
原作者:
...