Can Apple's silent push notifications launch my app in the background?

后端 未结 2 674
猫巷女王i
猫巷女王i 2021-01-06 01:48

According to Apple\'s documentation I can register for silent notification by adding \"content-available\" = 1 key-value in aps payload dictionary.

2条回答
  •  灰色年华
    2021-01-06 02:49

    I have faced a similar kind of scenario, I wrote this code to debug if my application is waking up on silent notification or not.

    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *str = [defaults objectForKey:@"key"];
        if (str  == nil) {
            str = @"didReceiveRemoteNotification";
        }else{
            str = [NSString stringWithFormat:@"%@, didReceiveRemoteNotification",str];
        }
        [defaults setObject:str forKey:@"key"];
        [defaults synchronize];
    }
    

    This code works like, if you app wakes up than you'll get callback in this method and the method name will be written in NSUserDefaults. So when you debug your app manually you can see the value of str string variable, if there is string didReceiveRemoteNotification than you'll get to know that you application has woke up.

    Note: This works only in background mode for me. I get value when I did not force close(terminate manually) my app but when I force close my app from app switcher I'll not get any value.

    I hope that works.

提交回复
热议问题