update sqlite db from push notification, app is cl

2019-05-29 19:00发布

问题:

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
{   
}
 }

回答1:

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