将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

update sqlite db from push notification, app is closed and not running in background iOS
在后台iOS中,从推送通知更新SQLite数据库,应用程序关闭且未运行

122 1

I am trying to update local sqlite database when notification received and app is closed/killed by user.everything works fine when app is background or active mode.

Reference : REFERENCE STACK LINK 1

REFERENCE STACK LINK 2

Here is code i am trying :


-(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);



 } 


 }



Question : why it is not working in application INACTIVE state.is there a solution to this issue?


- (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


{ 


}


 }



时间: 原作者:

100 3

-(void)application:(UIApplication )application didReceiveRemoteNotification :

Method gets called only if user will tap on notification banner only if you app is in background.if you app is killed then you need to handle tap of banner in

didFinishLaunchWithOptions

method.You can use the following code to know if you tap on banner and the app is killed.


//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); 


}



...